[¿ªÀÚÁÖ] ÀÌ ±ÛÀº CodeProject »çÀÌÆ®¿¡ Ivo Ivanov°¡ "API hooking revealed"¶ó´Â Á¦¸ñÀ¸·Î °ÔÀçÇÑ ±ÛÀÔ´Ï´Ù. Win32 ½Ã½ºÅÛ¿¡¼ API¸¦ ÈÄÅ·ÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ Àü¹ÝÀûÀÎ ±â¹ýµéÀ» ´Ù·ç°í ÀÖÀ¸¸ç ÀÚ¼¼ÇÑ ¼³¸íÀ̳ª Äڵ庸´Ù´Â °³³äÀûÀÎ ³»¿ëµéÀÌ ¸¹¾Æ Win32 SDK¿¡ Àͼ÷Ä¡ ¾ÊÀº °³¹ßÀڵ鵵 ½±°Ô Á¢±ÙÇÒ ¼ö ÀÖ´Â ÁÁÀº ±ÛÀÎ °Í °°½À´Ï´Ù. ¿ø·¡ ¼Ò½º ÆÄÀÏÀÌ ÇÔ²² ÷ºÎµÇ¾î °ÔÀçµÈ ±ÛÀε¥ ÀÌ ÆÄÀÏÀ» ¹ø¿ªÇÑ ±Û°ú ÇÔ²² ¿Ã¸®´Â °ÍÀº Ȥ½Ã ÀúÀÛ±Ç ¹®Á¦°¡ µÉ ¼öµµ ÀÖ´Ù°í »ý°¢µÇ¾î µû·Î ÷ºÎÇÏÁö ¾Ê¾Ò½À´Ï´Ù. ¼Ò½º ÆÄÀÏÀÌ ÇÊ¿äÇϽŠºÐÀº ¿ø¹®ÀÎ "API hooking revealed"¿¡¼ ´Ù¿î¹Þ¾Æ »ç¿ëÇϽñ⠹ٶø´Ï´Ù.
ÀÌ ±ÛÀ» Àаí Á¦´ë·Î ÀÌÇØÇÏ·Á¸é ÃÖ¼ÒÇÑ À©µµ¿ìÁî ¸Þ½ÃÁö ÈÄÅ·°ú DLLÀÇ ±âº» ±¸Á¶¿¡ ´ëÇؼ´Â ¾Ë¾Æ¾ß ÇÕ´Ï´Ù. ÀÌ·± ³»¿ëµéÀº MSDN¿¡ Àß ¼³¸íµÇ¾î ÀÖ°í codeproject, codeguru, devpia µîÀÇ »çÀÌÆ®¿¡µµ ÁÁÀº ±ÛµéÀÌ ¸¹ÀÌ °ÔÀçµÇ¾î ÀÖÀ¸´Ï Âü°íÇϽñ⠹ٶø´Ï´Ù.
¾î¶² À©µµ¿ìÁî ¾îÇø®ÄÉÀ̼ÇÀ» °³¹ßÇϱâ À§Çؼ ¿ì¸®µéÀº ¿©·¯ Á¾·ùÀÇ ¾ð¾î³ª µµ±¸µéÀ» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. Visual Basic, Visual C++, Delphi, C++ Builder, PowerBuilder µî ¼ö¸¹Àº ¾ð¾î¿Í µµ±¸µéÀÌ ÀÖ½À´Ï´Ù.
ÀÌ·± ÇÁ·Î±×·¥µé¿¡¼ ¾î¶² ÀÛ¾÷À» ¼öÇàÇϱâ À§Çؼ »ç¿ëÇÏ´Â ¹æ¹ýÀº ¼·Î ´Ù¸¦Áö¶óµµ ±× ³»ºÎ·Î µé¾î°¡ º¸¸é °á±¹¿¡´Â À©µµ¿ìÁî ¿î¿µÃ¼Á¦¿¡¼ Á¦°øÇÏ´Â API¸¦ È£ÃâÇÏ°Ô µË´Ï´Ù. ¿¹¸¦ µé¾î ȸ鿡 ¹®ÀÚ¿À» Ãâ·ÂÇÏ·Á°í ÇÑ´Ù¸é MFC¿¡¼´Â CDC::DrawText¸¦ »ç¿ëÇÏ°í Dephi¿¡¼´Â TCanvas::TextOutÀ» »ç¿ëÇÕ´Ï´Ù. µµ±¸¸¶´Ù »ç¿ëÇÏ´Â ÇüÅ´ ÀÌ·¸°Ô ´Ù¸£Áö¸¸ °á±¹¿¡´Â TextOutA/W API¸¦ È£ÃâÇÕ´Ï´Ù.
TextOutA/W API´Â gdi32.dll¿¡ ±¸ÇöµÇ¾î ÀÖ´Â ÇÔ¼öÀÌ°í ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â APIÀÔ´Ï´Ù. À©µµ¿ìÁî´Â ±âº»ÀûÀ¸·Î 3°³ÀÇ DLL-kernel32.dll, user32.dll, gdi32.dll-¿¡ ´ëºÎºÐÀÇ API¸¦ ±¸ÇöÇÏ¿© Á¦°øÇÏ°í ¾îÇø®ÄÉÀ̼ÇÀº ½ÇÇà½Ã ÀÚ½ÅÀÇ ÇÁ·Î¼¼½º ÁÖ¼Ò °ø°£À¸·Î À̵é DLLÀ» ¸ÅÇÎÇÑ ÈÄ »ç¿ëÇÕ´Ï´Ù.
API ÈÄÅ·Àº ¾î¶² ÇÁ·Î±×·¥¿¡¼ API È£ÃâÀ» ÇÏ´Â °ÍÀ» °¡·Îä¼ °³¹ßÀÚ°¡ ¸¸µç ÇÁ·Î±×·¥ÀÇ ÇÔ¼ö°¡ ó¸®ÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â ¸ÞÄ«´ÏÁòÀ» ¸»ÇÕ´Ï´Ù. ¹Ýµå½Ã À©µµ¿ìÁîÀÇ APIÀÏ ÇÊ¿ä´Â ¾ø°í ´ÜÁö DLL¿¡¼ ±¸ÇöÇÏ¿© Á¦°øÇÏ´Â ÇÔ¼öÀÌ¸é µË´Ï´Ù.
API ÈÄÅ·Àº ¾î¶² ¾ð¾î·Î °³¹ßµÈ ÇÁ·Î±×·¥¿¡µµ Àû¿ëµÉ ¼ö ÀÖÀ¸¸ç µð¹ö±ëÀ̳ª Æ®·¹ÀÌ½Ì ÀÛ¾÷, ¸ð´ÏÅ͸µ ÀÛ¾÷ µî¿¡ »ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç ¼Ò½º Äڵ尡 ¾ø´Â ÇÁ·Î±×·¥¿¡ ±â´ÉÀ» Ãß°¡Çϱâ À§ÇÑ ¿ëµµ·Îµµ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×¿Ü¿¡µµ ¿©·¯°¡Áö ¿ëµµ·Î »ç¿ëÇÒ ¼ö ÀÖÁö¸¸ ±»ÀÌ ÀÌ·± ÇÁ·Î±×·¥À» °³¹ßÇÒ °èȹÀÌ ¾Æ´Ï´õ¶óµµ À©µµ¿ìÁî ÇÁ·Î±×·¥ÀÇ ±âº»ÀûÀÎ ¿ø¸®¸¦ ÀÌÇØÇϴµ¥ ¸¹Àº µµ¿òÀÌ µË´Ï´Ù.
API ÈÄÅ·À» Àû¿ëÇÏ¿© ¸¸µç ÇÁ·Î±×·¥ Áß¿¡ ÁÖÀ§¿¡¼ ÀϹÝÀûÀ¸·Î º¼ ¼ö ÀÖ´Â °ÍÀ¸·Î´Â myQuickFind, ¾Æ·¡ÇѱۻçÀü°ú °°Àº ÀüÀÚ »çÀü ÇÁ·Î±×·¥ÀÌ ÀÖ½À´Ï´Ù. ÀÌ·± ÇÁ·Î±×·¥µéÀº ¸ðµÎ ´Ù¸¥ À©µµ¿ìÀÇ ¸¶¿ì½º Ä¿¼ ¹Ø¿¡ ÀÖ´Â ´Ü¾î¸¦ ÀνÄÇÒ ¼ö ÀÖ´Â ±â´ÉÀ» °¡Áö°í Àִµ¥ ÀÌ°ÍÀº À©µµ¿ìÁî ¸Þ½ÃÁö ÈÄÅ·°ú API ÈÄÅ·(TextOutA/W)À» ÀÌ¿ëÇÑ °ÍÀÔ´Ï´Ù.
¶ÇÇÑ Å©·¡Å·À̳ª ½ºÆÄÀÌ¿þ¾î¸¦ °³¹ßÇϴµ¥ ¾Ç¿ëµÉ ¼öµµ ÀÖ°í ¹Ý´ë·Î ¹æÁöÇÏ´Â ¿ëµµ·Îµµ »ç¿ëµÉ ¼ö ÀÖ½À´Ï´Ù. ¾îÂîµÇ¾úµç °£¿¡ ÀÌ ±ÛÀÌ À©µµ¿ìÁî ȯ°æ¿¡¼ °³¹ßÀ» ÇϽô ¸¹Àº ºÐµé¿¡°Ô µµ¿òÀÌ µÇ±â¸¦ ¹Ù¶ø´Ï´Ù.
¹ø¿ªÇؼ ±ÛÀ» ¿Ã¸®´Â °ÍÀÌ Ã³À½ÀÌ¾î¼ ¾î»öÇÑ ºÎºÐµéÀÌ ¸¹½À´Ï´Ù. °Ô´Ù°¡ ÀúÀÚ°¡ ½°Ç¥¸¦ »ý·«ÇÏ´Â µ¿°Ý ¸í»ç ±¸¹®À̳ª ºÐ»ç ±¸¹®À» ¸¹ÀÌ »ç¿ëÇÏ¿´°í ¹ø¿ªÀÌ Ç¥ÁØȵÇÁö ¾ÊÀº °³¹ß °ü·Ã ¿ë¾îµéµµ ¸¹¾Æ ´õ¿í ±×·¸½À´Ï´Ù. ±×·¡¼ ¿ø¹®°ú ¹ø¿ª¹®À» ÇÔ²² ¿Ã·È°í ¹ø¿ªÀÌ ¹ÌºñÇϰųª ¾î»öÇÑ ºÎºÐ¿¡´Â [¿ªÀÚÁÖ]¸¦ ´Þ¾Æ ³õ¾Ò½À´Ï´Ù. ±×·¡µµ ÀÌ»óÇÑ ºÎºÐµéÀº ÄÚ¸àÆ®¸¦ ´Þ¾Æ ÁÖ½Ã¸é °¡´ÉÇÏ¸é ¼öÁ¤Çؼ ´Ù½Ã ¿Ã¸®µµ·Ï ÇÏ°Ú½À´Ï´Ù.
Intercepting Win32 API calls has always been a challenging subject among most of the Windows developers and I have to admit, it's been one of my favorite topics. The term Hooking represents a fundamental technique of getting control over a particular piece of code execution. It provides an straightforward mechanism that can easily alter the operating system's behavior as well as 3rd party products, without having their source code available.
Win32 API È£ÃâÀ» °¡·Îä´Â °ÍÀº ´ë´Ù¼öÀÇ À©µµ¿ìÁî °³¹ßÀÚµé »çÀÌ¿¡¼ Ç×»ó µµÀüÇÏ´Â °úÁ¦À̾úÀ¸¸ç ³»°¡ °¡Àå ÁÁ¾ÆÇÏ´Â ÁÖÁ¦ ÁßÀÇ Çϳª°¡ µÇ¾ú´Ù. ÈÄÅ·À̶ó´Â ´Ü¾î´Â ÄÚµå ½ÇÇàÀÇ Æ¯Á¤ ºÎºÐÀ» Á¦¾îÇÒ ¼ö ÀÖ´Â ±â¹Ý ±â¼úÀ» ÀǹÌÇÑ´Ù. ÈÄÅ·Àº ½áµå ÆÄƼ Á¦Ç°»Ó ¾Æ´Ï¶ó ¿î¿µÃ¼Á¦ÀÇ µ¿ÀÛ±îÁöµµ ¼Ò½º ÄÚµå ¾øÀÌ ½±°Ô ¹Ù²Ü ¼ö ÀÖ´Â °£ÆíÇÑ ¸ÞÄ«´ÏÁòÀ» Á¦°øÇÑ´Ù.
Many modern systems draw the attention to their ability to utilize existing Windows applications by employing spying techniques. A key motivation for hooking, is not only to contribute to advanced functionalities, but also to inject user-supplied code for debugging purposes.
¸¹Àº Çö´ëÀûÀÎ ½Ã½ºÅÛµéÀÌ ½ºÆÄÀÌ ±â¼úÀ» »ç¿ëÇÏ¿© ±âÁ¸ÀÇ À©µµ¿ìÁî ¾îÇø®ÄÉÀ̼ÇÀ» È°¿ëÇÏ´Â °Í¿¡ °ü½ÉÀ» ±â¿ïÀÌ°í ÀÖ´Ù. ÈÄÅ·À» ¿¬±¸ÇÏ´Â ÁÖ¿ä µ¿±â´Â Çâ»óµÈ ±â´ÉÀ» Á¦°øÇÏ´Â °Í »Ó ¾Æ´Ï¶ó µð¹ö±ëÀ» ¸ñÀûÀ¸·Î »ç¿ëÀÚ Á¤ÀÇ Äڵ带 ħÅõ½ÃÅ°´Â °Í¿¡ ÀÖ´Ù.
Unlike some relatively "old" operating systems like DOS and Windows 3.xx, the present Windows OS as NT/2K and 9x provide sophisticated mechanisms to separate address spaces of each process. This architecture offers a real memory protection, thus no application is able to corrupt the address space of another process or in the worse case even to crash the operating system itself. This fact makes a lot harder the development of system-aware hooks.
DOS, À©µµ¿ìÁî3.XX¿Í °°ÀÌ »ó´ëÀûÀ¸·Î ¿À·¡µÈ ¿î¿µÃ¼Á¦¿Í ´Þ¸®, NT/2K,9x¿Í °°Àº ÇöÀçÀÇ À©µµ¿ìÁî OS´Â °¢°¢ÀÇ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£À» ºÐ¸®ÇÏ´Â Á¤±³ÇÑ ¸ÞÄ«´ÏÁòÀ» Á¦°øÇÑ´Ù. ÀÌ·¯ÇÑ ±¸Á¶´Â ½ÇÁ¦ ¸Þ¸ð¸® º¸È£¸¦ Á¦°øÇÏ¿© ¾î¶°ÇÑ ¾îÇø®ÄÉÀ̼ǵµ ´Ù¸¥ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£À» º¯Çü½ÃÅ°°Å³ª ´õ ³ª¾Æ°¡ ¿î¿µ üÁ¦ ÀÚü¸¦ ¸Á°¡¶ß¸®Áö ¸øÇϵµ·Ï ÇÑ´Ù. ÀÌ·¯ÇÑ ÀÌÀ¯·Î ½Ã½ºÅÛ ÈÄÅ·ÀÇ °³¹ßÀº ´õ¿í ¾î·Á¿öÁ³´Ù.
My motivation for writing this article was the need for a really simple hooking framework, that will offer an easy to use interface and ability to capture different APIs. It intends to reveal some of the tricks that can help you to write your own spying system. It suggests a single solution how to build a set for hooking Win32 API functions on NT/2K as well as 98/Me (shortly named in the article 9x) family Windows. For the sake of simplicity I decided not to add a support do UNICODE. However, with some minor modifications of the code you could easily accomplish this task.
ÀÌ ±ÛÀ» ¾²°Ô µÈ µ¿±â´Â »ç¿ëÇϱ⠽±°í ´Ù¸¥ APIµéÀ» °¡·Îç ¼ö ÀÖ´Â ¸Å¿ì °£´ÜÇÑ ÈÄÅ· ÇÁ·¹ÀÓ¿÷¿¡ ´ëÇÑ Çʿ伺 ¶§¹®ÀÌ´Ù. ÀÌ ±Û¿¡¼ ½ºÆÄÀÌ ½Ã½ºÅÛÀ» ±¸ÃàÇϱâ À§ÇØ ÇÊ¿äÇÑ ¸î°¡Áö Æ®¸¯µéÀ» ¹àÈú °èȹÀÌ´Ù. ÀÌ ±ÛÀº 98/Me¿Í NT/2K ¿¡¼ ¸ðµÎ »ç¿ëÇÒ ¼ö ÀÖ´Â Win32 API ÇÔ¼ö ÈÄÅ·¿¡ ´ëÇÑ ÇϳªÀÇ ¼Ö·ç¼ÇÀ» Á¦¾ÈÇÑ´Ù. ´Ü¼øÇÏ°Ô Çϱâ À§ÇÏ¿© UNICODE¿¡ ´ëÇÑ Áö¿øÀº Ãß°¡ÇÏÁö ¾Ê±â·Î ÇÏ¿´´Ù. ÇÏÁö¸¸ Äڵ带 ¾à°£ ¼öÁ¤Çϸé UNICODE¿¡ ´ëÇÑ Áö¿øÀ» ½±°Ô ´Þ¼ºÇÒ ¼ö ÀÖÀ» °ÍÀÌ´Ù.
Spying of applications provides many advantages:
¾îÇø®ÄÉÀ̼ÇÀ» ½ºÆÄÀÌÇÏ´Â °ÍÀº ´ÙÀ½°ú °°Àº ¸¹Àº ÀÌÁ¡À» Á¦°øÇÑ´Ù:
API ÇÔ¼ö È£ÃâÀ» Á¦¾îÇÒ ¼ö ÀÖ´Â ´É·ÂÀº API È£Ãâ½Ã ¹ß»ýÇÏ´Â º¸ÀÌÁö ¾Ê´Â ƯÁ¤ ÇàÀ§¸¦ °³¹ßÀÚ°¡ ÃßÀûÇÒ ¼ö ÀÖµµ·Ï µµ¿ÍÁØ´Ù. ¶ÇÇÑ Ç×»ó Áö³ªÄ¥ ¼ö ÀÖ´Â ¹®Á¦Á¡µéÀ» º¸°íÇÒ »Ó ¾Æ´Ï¶ó ¸Å°³º¯¼ö¿¡ ´ëÇÑ Á¾ÇÕÀûÀÎ °ËÁõÀ» ÇÒ ¼öµµ ÀÖ´Ù. ¿¹¸¦ µé¸é ¸Þ¸ð¸®¿Í °ü·ÃµÈ API ÇÔ¼öµéÀ» °¨½ÃÇÏ¿© ¸®¼Ò½º ´©ÃâÀ» Àâ¾Æ³»´Â °Í¿¡ ¸Å¿ì À¯¿ëÇÏ´Ù.
µð¹ö±ëÀ» À§ÇÑ Ç¥ÁØÀûÀÎ ¹æ¹ýµéÀ» Á¦¿ÜÇϸé API ÈÄÅ·Àº °¡Àå ÀαâÀÖ´Â µð¹ö±ë ¸ÞÄ«´ÏÁòÀÇ Çϳª¶ó´Â Æò°¡¸¦ ¹Þ´Â´Ù. ¸¹Àº °³¹ßÀÚµéÀÌ API ÈÄÅ· ±â¼úÀ» ÄÞÆ÷³ÑÆ®ÀÇ ¼·Î ´Ù¸¥ ±¸Çöµé°ú ±×µéÀÇ °ü°è¸¦ ÆľÇÇϱâ À§ÇØ »ç¿ëÇÑ´Ù. API °¡·Îä±â´Â ÀÌÁø ½ÇÇà ÆÄÀÏ¿¡ ´ëÇÑ Á¤º¸¸¦ ¾òÀ» ¼ö ÀÖ´Â ¸Å¿ì °·ÂÇÑ ¹æ¹ýÀÌ´Ù.
°¡²û °³¹ßÀÚµéÀº ¿î¿µÃ¼Á¦¿¡ ´ëÇØ ÀÌÇØÇϱ⸦ °¥¸ÁÇÏ°í "µð¹ö°Å"ÀÇ ¿ªÇÒÀ» ÇÏ°Ô µÈ´Ù. ÈÄÅ·Àº ¹®¼ÈµÇÁö ¾Ê¾Ò°Å³ª ºó¾àÇÏ°Ô ¹®¼ÈµÈ API¸¦ Çؼ®Çϴµ¥ ¸Å¿ì À¯¿ëÇÏ´Ù.
¿ÜºÎÀÇ À©µµ¿ìÁî ¾îÇø®ÄÉÀ̼ǿ¡ »ç¿ëÀÚ ¸ðµâÀ» »ðÀÔÇÏ¿© ±âÁ¸¿¡ Á¦°øµÇ´ø ±â´ÉÀ» È®ÀåÇÏ´Â, ÈÄÅ©¸¦ ħÅõ½ÃÄÑ ±âÁ¸ÀÇ ÄÚµå ½ÇÇàÀ» ÀçÆí¼ºÇÏ´Â °ÍÀº ±âÁ¸ ¸ðµâÀÇ ±â´ÉÀ» ¼öÁ¤ÇÏ°í È®ÀåÇÏ´Â ½¬¿î ¹æ¹ýÀ» Á¦°øÇÒ ¼ö ÀÖ´Ù.(???) ¿¹À» µé¸é ¸¹Àº ½áµå ÆÄƼ Á¦Ç°µéÀÌ Æ¯º°ÇÑ º¸¾ÈÀû ¿ä±¸ »çÇ×¾øÀÌ Æ¯Á¤ ±â´ÉÀ» Çϵµ·Ï ¼öÁ¤µÇ¾î¾ß ÇÑ´Ù. ¾îÇø®ÄÉÀÌ¼Ç ½ºÆÄÀÌ´Â °³¹ßÀÚ°¡ ¿øº» API ÇÔ¼öÀÇ È£Ãâ Àü,ÈÄ¿¡ ¼¼·ÃµÈ 󸮸¦ Ãß°¡ÇÒ ¼ö ÀÖµµ·Ï ÇØÁØ´Ù. ÀÌ·¯ÇÑ ´É·ÂÀº ÀÌ¹Ì ÄÄÆÄÀÏµÈ ÄÚµåÀÇ µ¿ÀÛÀ» º¯°æÇϴµ¥ ¸Å¿ì À¯¿ëÇÏ´Ù.
There are few important decisions that have to be made, before you start implementing any kind of API hooking system. First of all, you should determine whether to hook a single application or to install a system-aware engine. For instance if you would like to monitor just one application, you don't need to install a system-wide hook but if your job is to track down all calls to TerminateProcess()
or WriteProcessMemory()
the only way to do so is to have a system-aware hook. What approach you will choose depends on the particular situation and addresses specific problems.
API ÈÄÅ· ½Ã½ºÅÛ ±¸ÇöÀ» ½ÃÀÛÇϱâ Àü¿¡ °áÁ¤ÇØ¾ß ÇÒ ¸î°¡Áö Áß¿äÇÑ »çÇ×µéÀÌ ÀÖ´Ù. ¹«¾ùº¸´Ù ¸ÕÀú ÇϳªÀÇ ¾îÇø®ÄÉÀ̼ÇÀ» ÈÄÅ·ÇÒ °ÍÀΰ¡ ¾Æ´Ï¸é ½Ã½ºÅÛ Àü¿ª ¿£ÁøÀ» ¼³Ä¡ÇÒ °ÍÀΰ¡¸¦ °áÁ¤ÇØ¾ß ÇÑ´Ù. ¿¹¸¦ µé¾î ÇϳªÀÇ ¾îÇø®ÄÉÀ̼Ǹ¸À» °¨½ÃÇϱ⠿øÇÑ´Ù¸é ½Ã½ºÅÛ Àü¿ª ÈÄÅ©¸¦ ¼³Ä¡ÇÒ ÇÊ¿ä°¡ ¾øÁö¸¸ TerminateProcess()
ÇÔ¼ö³ª WriteProcessMemory()
ÇÔ¼ö¿¡ ´ëÇÑ ¸ðµç È£ÃâÀ» ÃßÀûÇÏ´Â ¾÷¹«¶ó¸é ½Ã½ºÅÛ Àü¿ª ÈÄÅ©¸¦ ¼³Ä¡Çؾ߸¸ ÇÑ´Ù. ¾î¶² Á¢±Ù ¹æ¹ýÀ» ¼±ÅÃÇϴ°¡´Â ¾î¶°ÇÑ »óȲÀΰ¡¿¡ ´Þ·Á ÀÖÀ¸¸ç Á¢±Ù ¹æ¹ýµéÀº °¢°¢ Ưº°ÇÑ ¹®Á¦Á¡µéÀ» °¡Áö°í ÀÖ´Ù.
Usually a Hook system is composed of at least two parts - a Hook Server and a Driver. The Hook Server is responsible for injecting the Driver into targeted processes at the appropriate moment. It also administers the driver and optionally can receive information from the Driver about its activities whereas the Driver module that performs the actual interception.
This design is rough and beyond doubt doesn't cover all possible implementations. However it outlines the boundaries of a hook framework.
Once you have the requirement specification of a hook framework, there are few design points you should take into account:
I hope next the few sections will provide answers to those issues.
ÀϹÝÀûÀ¸·Î ÈÄÅ© ½Ã½ºÅÛÀº ÃÖ¼ÒÇÑ 2ºÎºÐ, ÈÄÅ© ¼¹ö¿Í µå¶óÀ̹ö·Î ±¸¼ºµÈ´Ù. ÈÄÅ© ¼¹ö´Â ÀûÀýÇÑ ½ÃÁ¡¿¡ ¸ñÇ¥·Î ÇÏ´Â ÇÁ·Î¼¼½º¿¡ µå¶óÀ̹ö¸¦ ħÅõ½ÃÅ°´Â ±â´ÉÀ» ÇÑ´Ù. ¶ÇÇÑ µå¶óÀ̹ö¸¦ °ü¸®ÇÏ¸ç ¼±ÅÃÀûÀ¸·Î µå¶óÀ̹ö¿¡¼ È°µ¿ Á¤º¸¸¦ ¹ÞÀ» ¼ö ÀÖÀ¸¸ç µå¶óÀ̹ö ¸ðµâÀº ½ÇÁúÀûÀÎ °¡·Îä±â¸¦ ¼öÇàÇÑ´Ù.
ÀÌ·¯ÇÑ ¼³°è´Â ¼¼·ÃµÇÁö ¾Ê¾Ò°í ÀǽÉÇÒ ¹Ù ¾øÀÌ °¡´É¼ºÀÖ´Â ¸ðµç ±¸ÇöÀ» ó¸®ÇÒ ¼ö ¾ø´Ù. ÇÏÁö¸¸ ÈÄÅ© ÇÁ·¹ÀÓ¿÷ÀÇ ¹üÀ§ÀÇ À±°ûÀº Àâ¾ÆÁØ´Ù.
ÀÏ´Ü ÈÄÅ© ÇÁ·¹ÀÓ¿÷ÀÇ ¿ä±¸ »çÇ×ÀÌ Á¤ÀÇµÇ¸é ±â·ÏÇØ ³õ¾Æ¾ß ÇÒ ¸î°¡Áö ¼³°è Æ÷ÀÎÆ®°¡ ÀÖ´Ù:
´ÙÀ½ÀÇ ¸î°³ ¼½¼Ç¿¡¼ ÀÌ·¯ÇÑ »çÇ׵鿡 ´ëÇØ ¼³¸íÇÏ°Ú´Ù.
¾î¶² DLLÀ» USER32.DLL°ú ¸µÅ©µÇ¾î ÀÖ´Â ÇÁ·Î¼¼½º¿¡ ħÅõ½ÃÅ°±â À§Çؼ´Â ¾Æ·¡ÀÇ ·¹Áö½ºÆ®¸® Å°ÀÇ °ª¿¡ DLLÀÇ À̸§À» Ãß°¡Çϱ⸸ ÇÏ¸é µÈ´Ù.
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs
Its value contains a single DLL name or group of DLLs separated either by comma or spaces. According to MSDN documentation [7], all DLLs specified by the value of that key are loaded by each Windows-based application running within the current logon session. It is interesting that the actual loading of these DLLs occurs as a part of USER32's initialization. USER32 reads the value of mentioned registry key and calls LoadLibrary()
for these DLLs in its DllMain
code. However this trick applies only to applications that use USER32.DLL. Another restriction is that this built-in mechanism is supported only by NT and 2K operating systems. Although it is a harmless way to inject a DLL into a Windows processes there are few shortcomings:
ÀÌ Å°ÀÇ °ª¿¡´Â ÇϳªÀÇ DLL À̸§¸¸ µî·ÏÇÒ ¼öµµ ÀÖ°í ÄÞ¸¶³ª ½ºÆäÀ̽º·Î ±¸ºÐÇÏ¿© ¿©·¯ °³ÀÇ DLLµéÀ» µî·ÏÇÒ ¼öµµ ÀÖ´Ù. MSDN ¹®¼ [7]¿¡ ÀÇÇϸé ÀÌ Å°ÀÇ °ªÀ¸·Î µî·ÏµÈ DLLµéÀº ÇöÀçÀÇ ·Î±×¿Â ¼¼¼Ç ³»¿¡¼ À©µµ¿ìÁî ±â¹ÝÀÇ ¾îÇø®ÄÉÀ̼ÇÀÌ ½ÇÇàµÉ ¶§¸¶´Ù ·ÎµåµÈ´Ù. Èï¹Ì·Ó°Ôµµ ÀÌ DLLµéÀÇ ½ÇÁ¦ÀûÀÎ ·Îµå´Â USER32ÀÇ ÃʱâÈ °úÁ¤ÀÇ ÀϺκÐÀ¸·Î¼ ¼öÇàµÈ´Ù. USER32´Â »ó±âÀÇ ·¹Áö½ºÆ®¸® Å°ÀÇ °ªÀ» Àоî DllMain
¾È¿¡¼ À̵é DLL¿¡ ´ëÇØ LoadLibrary()
ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. ±×·¯³ª ÀÌ ¹æ¹ýÀº USER32.DLLÀ» »ç¿ëÇÏ´Â ¾îÇø®ÄÉÀ̼ǿ¡ ´ëÇؼ¸¸ Àû¿ëÇÒ ¼ö ÀÖ´Ù. ¶Ç´Ù¸¥ Á¦¾àÀº ÀÌ ¸ÞÄ«´ÏÁòÀÌ NT¿Í 2K ¿î¿µÃ¼Á¦¿¡ ´ëÇؼ¸¸ Áö¿øµÈ´Ù´Â °ÍÀÌ´Ù. ÀÌ ¹æ¹ýÀÌ ¾î¶² À©µµ¿ìÁî ÇÁ·Î¼¼½º¿¡ DLLÀ» ħÅõ½ÃÅ°´Â À¯¿ëÇÑ ¹æ¹ýÀÌÁö¸¸ ¸î°¡Áö ´ÜÁ¡µéÀÌ ÀÖ´Ù:
¸ñÇ¥·Î ÇÏ´Â ÇÁ·Î¼¼½º¿¡ DLLÀ» ħÅõ½ÃÅ°´Â ¸Å¿ì ÀαâÀÖ´Â ±â¼úÀº À©µµ¿ìÁî ÈÄÅ©¸¦ ÀÌ¿ëÇÏ´Â °ÍÀÌ´Ù. MSDN¿¡¼ ¼³¸íÇϵíÀÌ ÈÄÅ©´Â ½Ã½ºÅÛ »óÀÇ ¸Þ½ÃÁö ó¸® ¸ÞÄ«´ÏÁòÀ» °¡·Îä´Â °ÍÀÌ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº »ç¿ëÀÚ Á¤ÀÇ ÇÊÅÍ ÇÔ¼ö¸¦ ¼³Ä¡ÇÏ¿© ½Ã½ºÅÛ¿¡¼ÀÇ ¸Þ½ÃÁö ±³È¯À» °¨½ÃÇÏ°í ƯÁ¤ÇÑ Á¾·ùÀÇ ¸Þ½ÃÁö°¡ ¸ñÇ¥·Î ÇÏ´Â À©µµ¿ì¿¡ µµ´ÞÇϱâ Àü¿¡ ó¸®ÇÒ ¼ö ÀÖ´Ù.
A hook is normally implemented in a DLL in order to meet the basic requirement for system-wide hooks. The basic concept of that sort of hooks is that the hook callback procedure is executed in the address spaces of each hooked up process in the system. To install a hook you call SetWindowsHookEx()
with the appropriate parameters. Once the application installs a system-wide hook, the operating system maps the DLL into the address space in each of its client processes. Therefore global variables within the DLL will be "per-process" and cannot be shared among the processes that have loaded the hook DLL. All variables that contain shared data must be placed in a shared data section. The diagram bellow shows an example of a hook registered by Hook Server and injected into the address spaces named "Application one" and "Application two".
ÈÄÅ©´Â ½Ã½ºÅÛ Àü¿ª ÈÄÅ©ÀÇ ±âº» ¿ä±¸ »çÇ×À» ÃæÁ·½ÃÅ°±â À§Çؼ´Â º¸Åë DLL·Î ±¸ÇöµÈ´Ù. ÀÌ·¯ÇÑ Á¾·ùÀÇ ÈÄÅ©´Â ±âº»ÀûÀ¸·Î ÈÄÅ© Äݹé ÇÁ·Î½ÃÁ®°¡ ½Ã½ºÅÛ »ó¿¡¼ ÈÄÅ©µÈ °¢°¢ÀÇ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡¼ ½ÇÇàµÈ´Ù. ÈÄÅ©¸¦ ¼³Ä¡Çϱâ À§Çؼ´Â SetWindowsHookEx()
ÇÔ¼ö¸¦ ÀûÀýÇÑ ¸Å°³º¯¼ö¸¦ ÁÖ¾î È£ÃâÇÏ¿©¾ß ÇÑ´Ù. ÀÏ´Ü ¾îÇø®ÄÉÀ̼ÇÀÌ ½Ã½ºÅÛ Àü¿ª ÈÄÅ©¸¦ ¼³Ä¡ÇÏ¸é ¿î¿µÃ¼Á¦´Â DLLÀ» Ŭ¶óÀ̾ðÆ® ÇÁ·Î¼¼½ºÀÇ °¢°¢ÀÇ ÁÖ¼Ò °ø°£¿¡ ¸ÅÇÎÇÏ°Ô µÈ´Ù. ±×·¯¹Ç·Î DLL ³»ÀÇ Àü¿ª º¯¼ö´Â ÇÁ·Î¼¼½º¸¶´Ù Á¸ÀçÇÏ°Ô µÇ°í ÈÄÅ© DLLÀ» ÀûÀçÇÑ ÇÁ·Î¼¼½º »çÀÌ¿¡¼ °øÀ¯ÇÒ ¼ö ¾ø°Ô µÈ´Ù. µ¥ÀÌÅ͸¦ °øÀ¯ÇÏ´Â ¸ðµç º¯¼öµéÀº °øÀ¯ µ¥ÀÌÅÍ ¼½¼Ç¿¡ Á¤ÀǵǾî¾ß¸¸ ÇÑ´Ù. ¾Æ·¡ÀÇ ±×¸²Àº ÈÄÅ© ¼¹ö¿¡ ÀÇÇØ µî·ÏµÈ ÇϳªÀÇ ÈÄÅ©¿Í "Application one"°ú "Application two"¶ó´Â À̸§ÀÇ ÁÖ¼Ò °ø°£¿¡ ħÅõÇÑ ÈÄÅ©ÀÇ ¿¹¸¦ º¸¿©ÁØ´Ù.
Figure 1
A system-wide hook is registered just ones when SetWindowsHookEx()
is executed. If no error occurs a handle to the hook is returned. The returned value is required at the end of the custom hook function when a call to CallNextHookEx()
has to be made. After a successful call to SetWindowsHookEx()
, the operating system injects the DLL automatically (but not necessary immediately) into all processes that meet the requirements for this particular hook filter. Let's have a closer look at the following dummy WH_GETMESSAGE
filter function:
½Ã½ºÅÛ Àü¿ª ÈÄÅ©´Â SetWindowsHookEx()
ÇÔ¼ö°¡ ½ÇÇàµÉ ¶§ µî·ÏµÈ´Ù. ¿À·ù°¡ ¾øÀ¸¸é ÈÄÅ© ÇÚµéÀÌ ¹ÝȯµÈ´Ù. ¹Ýȯ°ªÀº »ç¿ëÀÚ Á¤ÀÇ ÈÄÅ© ÇÔ¼öÀÇ ³¡ºÎºÐ¿¡¼ CallNextHookEx()
ÇÔ¼ö¸¦ È£ÃâÇÒ ¶§ ÇÊ¿äÇÏ´Ù. SetWindowsHookEx()
ÇÔ¼ö°¡ ¼º°øÀûÀ¸·Î È£ÃâµÇ¸é ¿î¿µÃ¼Á¦´Â DLLÀ» Ưº°ÇÑ ÈÄÅ© ÇÊÅÍÀÇ ¿ä±¸ »çÇ×À» ÃæÁ·½ÃÅ°´Â ¸ðµç ÇÁ·Î¼¼½º¿¡ ÀÚµ¿ÀûÀ¸·Î(Áï½Ã´Â ¾Æ´Ô) ħÅõ½ÃŲ´Ù. ¾Æ·¡ÀÇ ¾Æ¹«°Íµµ ÇÏÁö ¾Ê´Â WH_GETMESSAGE
ÇÊÅÍ ÇÔ¼ö¸¦ º¸ÀÚ:
//---------------------------------------------------------------------------
// GetMsgProc
//
// Filter function for the WH_GETMESSAGE - it's just a dummy function
//---------------------------------------------------------------------------
LRESULT CALLBACK GetMsgProc(
int code, // hook code
WPARAM wParam, // removal option
LPARAM lParam // message
)
{
// We must pass the all messages on to CallNextHookEx.
return ::CallNextHookEx(sg_hGetMsgHook, code, wParam, lParam);
}
A system-wide hook is loaded by multiple processes that don't share the same address space.
½Ã½ºÅÛ Àü¿ª ÈÄÅ©´Â µ¿ÀÏÇÑ ÁÖ¼Ò °ø°£À» °øÀ¯ÇÏÁö ¾Ê´Â ¿©·¯ °³ÀÇ ÇÁ·Î¼¼½ºµé¿¡ ·ÎµåµÈ´Ù.
For instance hook handle sg_hGetMsgHook
, that is obtained by SetWindowsHookEx()
and is used as parameter in CallNextHookEx()
must be used virtually in all address spaces. It means that its value must be shared among hooked processes as well as the Hook Server application. In order to make this variable "visible" to all processes we should store it in the shared data section.
¿¹¸¦ µé¸é SetWindowsHookEx()
È£Ãâ·Î ¾ò¾îÁö°í CallNextHookEx()
È£ÃâÀÇ ¸Å°³º¯¼ö·Î »ç¿ëµÇ´Â sg_hGetMsgHook
´Â ¸ðµç ÁÖ¼Ò °ø°£¿¡¼ »ç¿ëµÈ´Ù. ÀÌ°ÍÀº ÀÌ °ªÀÌ ÈÄÅ© ¼¹ö ¾îÇø®ÄÉÀÌ¼Ç »Ó¸¸ ¾Æ´Ï¶ó ÈÄÅ©µÈ ÇÁ·Î¼¼½º »çÀÌ¿¡¼µµ °øÀ¯µÇ¾î¾ß ÇÑ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. ÀÌ º¯¼ö¸¦ ¸ðµç ÇÁ·Î¼¼½º¿¡¼ »ç¿ë °¡´ÉÇϵµ·Ï Çϱâ À§Çؼ´Â ÀÌ º¯¼ö¸¦ °øÀ¯ µ¥ÀÌÅÍ ¼½¼Ç¿¡ ÀúÀåÇÏ¿©¾ß¸¸ ÇÑ´Ù.
The following is an example of employing #pragma data_seg()
. Here I would like to mention that the data within the shared section must be initialized, otherwise the variables will be assigned to the default data segment and #pragma data_seg()
will have no effect.
¾Æ·¡ÀÇ ¿¹Á¦´Â #pragma data_seg()
ÀÇ »ç¿ëÀ» º¸¿©ÁØ´Ù. °øÀ¯ ¼½¼ÇÀÇ µ¥ÀÌÅ͵éÀº ¹Ýµå½Ã ÃʱâȵǾî¾ß ÇÏ¸ç ±×·¸Áö ¾ÊÀ¸¸é º¯¼öµéÀº µðÆúÆ® µ¥ÀÌÅÍ ¼¼±×¸ÕÆ®¿¡ ÇÒ´çµÇ¾î #pragma data_seg()
´Â ¾Æ¹«·± È¿°úµµ °®Áö ¾Ê°Ô µÈ´Ù´Â ½Ã½ÇÀ» °Á¶ÇÏ°í ½Í´Ù.
//--------------------------------------------------------------------------- // Shared by all processes variables //--------------------------------------------------------------------------- #pragma data_seg(".HKT") HHOOK sg_hGetMsgHook = NULL; BOOL sg_bHookInstalled = FALSE; // We get this from the application who calls SetWindowsHookEx()'s wrapper HWND sg_hwndServer = NULL; #pragma data_seg()You should add a SECTIONS statement to the DLL's DEF file as well
DLLÀÇ DEF ÆÄÀÏ¿¡µµ SECTIONS ¹®ÀåÀ» Ãß°¡ÇÏ¿©¾ß ÇÑ´Ù.
SECTIONS .HKT Read Write Sharedor use
#pragma comment(linker, "/section:.HKT, rws")
Once a hook DLL is loaded into the address space of the targeted process, there is no way to unload it unless the Hook Server calls UnhookWindowsHookEx()
or the hooked application shuts down. When the Hook Server calls UnhookWindowsHookEx()
the operating system loops through an internal list with all processes which have been forced to load the hook DLL. The operating system decrements the DLL's lock count and when it becomes 0, the DLL is automatically unmapped from the process's address space.
Here are some of the advantages of this approach:
UnhookWindowsHookEx()
ÈÄÅ© DLLÀÌ ¸ñÇ¥·Î ÇÏ´Â ÇÁ·Î¼¼½º¿¡ ÀÏ´Ü ·ÎµåµÇ¸é ÈÄÅ© ¼¹ö°¡ UnhookWindowsHookEx()
ÇÔ¼ö¸¦ È£ÃâÇϰųª ÈÄÅ©µÈ ¾îÇø®ÄÉÀ̼ÇÀ» Á¾·áÇϱâ Àü¿¡´Â ¾ð·ÎµåÇÒ ¹æ¹ýÀÌ ¾ø´Ù. ÈÄÅ© ¼¹ö°¡ UnhookWindowsHookEx()
ÇÔ¼ö¸¦ È£ÃâÇÏ¸é ¿î¿µÃ¼Á¦´Â ³»ºÎÀÇ ¸®½ºÆ®¸¦ ÅëÇØ ÈÄÅ© DLLÀ» ·ÎµåÇϵµ·Ï Áö½ÃÇß´ø ¸ðµç ÇÁ·Î¼¼½ºµéÀ» ¼øȸÇÏ°Ô µÈ´Ù. ¿î¿µÃ¼Á¦´Â DLLÀÇ ÂüÁ¶¼ö¸¦ °¨¼Ò½ÃÅ°°í ÂüÁ¶¼ö°¡ 0ÀÌ µÇ¸é DLLÀº ÀÚµ¿ÀûÀ¸·Î ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡¼ ¾ð¸ÅÇεȴÙ.
ÀÌ·¯ÇÑ Á¢±Ù ¹æ½ÄÀº ´ÙÀ½°ú °°Àº ÀÌÁ¡ÀÌ °¡Áø´Ù:
UnhookWindowsHookEx()
ÇÔ¼ö¸¦ È£ÃâÇÏ°Ô µÇ¸é DLLÀ» ¾ð·ÎµåÇÒ ¼ö ÀÖ´Ù. Although I consider Windows Hooks as very handy injection technique, it comes with its own disadvantages:
À©µµ¿ìÁî ÈÄÅ©°¡ ¸Å¿ì Æí¸®ÇÑ Ä§Åõ ±â¼úÀÌÁö¸¸ ´ÙÀ½°ú °°Àº ´ÜÁ¡ÀÌ ÀÖ´Ù:
CreateRemoteThread()
API function (CreateRemoteThread()
ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ DLL ħÅõ)NULL
without doing anything. ÀÌ°ÍÀÌ ³»°¡ °¡Àå ¼±È£ÇÏ´Â ¹æ¹ýÀÌ´Ù. ºÒÇàÈ÷µµ ÀÌ ¹æ¹ýÀº ¿ÀÁ÷ NT¿Í 2K ¿î¿µÃ¼Á¦¿¡¼¸¸ Áö¿øµÈ´Ù. À©µµ¿ìÁî 9x¿¡¼µµ ÀÌ API¸¦ È£ÃâÇÒ ¼ö ÀÖÁö¸¸ ¾Æ¹« °Íµµ ÇÏÁö ¾Ê°í ±×³É NULL
À» ¹ÝȯÇÑ´Ù.
Injecting DLLs by remote threads is Jeffrey Ritcher's idea and is well documented in his article [9] "Load Your 32-bit DLL into Another Process's Address Space Using INJLIB".
¸®¸ðÆ® ¾²·¹µå¸¦ ÀÌ¿ëÇÏ¿© DLLÀ» ħÅõ½ÃÅ°´Â °ÍÀº Jeffrey RitcherÀÇ ¾ÆÀ̵ð¾îÀÌ¸ç ±×ÀÇ ±â»çÀÎ [9] "Load Your 32-bit DLL into Another Process's Address Space Using INJLIB"¿¡ Àß ¼³¸íµÇ¾î ÀÖ´Ù.
The basic concept is quite simple, but very elegant. Any process can load a DLL dynamically using LoadLibrary()
API. The issue is how do we force an external process to call LoadLibrary()
on our behalf, if we don't have any access to process's threads? Well, there is a function, called CreateRemoteThread()
that addresses creating a remote thread. Here comes the trick - have a look at the signature of thread function, whose pointer is passed as parameter (i.e. LPTHREAD_START_ROUTINE
) to the CreateRemoteThread()
:
±âº» °³³äÀº »ó´çÈ÷ ´Ü¼øÇÏÁö¸¸ ±²ÀåÈ÷ ¸ÚÁö´Ù. ¾î¶² ÇÁ·Î¼¼½ºµµ LoadLibrary()
API¸¦ È£ÃâÇÏ¿© DLLÀ» µ¿ÀûÀ¸·Î ·ÎµåÇÒ ¼ö ÀÖ´Ù. ¹®Á¦´Â ¾î¶»°Ô ÇÁ·Î¼¼½ºÀÇ ¾²·¹µå¿¡ ´ëÇØ ÀüÇô Á¢±ÙÀ» ÇÏÁö ¸øÇÏ¸é¼ ¿ÜºÎÀÇ ÇÁ·Î¼¼½º°¡ ÀûÀýÇÏ°Ô LoadLibrary()
ÇÔ¼ö¸¦ È£ÃâÇϵµ·Ï ¸¸µå´Â°¡ ÇÏ´Â °ÍÀÌ´Ù. ÇϳªÀÇ ¸®¸ðÆ® ¾²·¹µå¸¦ »ý¼ºÇÏ´Â CreateRemoteThread()
ÇÔ¼ö°¡ ±× ÇØ´äÀÌ´Ù. ¿©±â¼ ¾à°£ÀÇ ¼ÓÀÓ¼ö°¡ ÇÊ¿äÇÏ´Ù. - ¾²·¹µå ÇÔ¼öÀÇ ¿øÇüÀ» º¸¶ó. ÀÌ ÇÔ¼öÀÇ Æ÷ÀÎÅÍ°¡ ¸Å°³º¯¼ö(LPTHREAD_START_ROUTINE
)·Î CreateRemoteThread()
ÇÔ¼ö¿¡ ³Ñ°ÜÁø´Ù:
DWORD WINAPI ThreadProc(LPVOID lpParameter);And here is the prototype of
LoadLibrary
API
±×¸®°í ¿©±â LoadLibrary
APIÀÇ ¿øÇüÀÌ ÀÖ´Ù.
HMODULE WINAPI LoadLibrary(LPCTSTR lpFileName);
WINAPI
, they both accept one parameter and the LoadLibrary()
as thread function, which will ±×·¸´Ù, ÀÌ ÇÔ¼öµéÀº µ¿ÀÏÇÑ ÇüŸ¦ Áö´Ñ´Ù. ÀÌ ÇÔ¼öµéÀº °°Àº È£Ãâ ±Ô¾à WINAPI
¸¦ »ç¿ëÇÏ°í, ÇϳªÀÇ ¸Å°³º¯¼ö¸¦ ¹Þ¾Æ µ¿ÀÏÇÑ Å©±âÀÇ °ªÀ» ¹ÝȯÇÑ´Ù. ÀÌ·¯ÇÑ ÀÏÄ¡Á¡µéÀº LoadLibrary()
ÇÔ¼ö¸¦ ¾²·¹µå ÇÔ¼ö·Î »ç¿ëÇÏ¿© ¸®¸ðÆ® ¾²·¹µå°¡ »ý¼ºµÈ ÈÄ¿¡ ½ÇÇà½Ãų ¼ö°¡ ÀÖ´Ù´Â ÈùÆ®¸¦ ÁØ´Ù. ¾Æ·¡ÀÇ ¿¹Á¦ Äڵ带 »ìÆ캸ÀÚ.
hThread = ::CreateRemoteThread( hProcessForHooking, NULL, 0, pfnLoadLibrary, "C:\\HookTool.dll", 0, NULL);
By using GetProcAddress()
API we get the address of the LoadLibrary()
API. The dodgy thing here is that Kernel32.DLL is mapped always to the same address space of each process, thus the address of LoadLibrary()
function has the same value in address space of any running process. This ensures that we pass a valid pointer (i.e. pfnLoadLibrary
) as parameter of CreateRemoteThread()
.
GetProcAddress()
API¸¦ »ç¿ëÇÏ¿© ¿ì¸®´Â LoadLibrary()
APIÀÇ ÁÖ¼Ò¸¦ ±¸ÇÒ ¼ö°¡ ÀÖ´Ù. ¿©±â¼ ÀÌ»óÇÑ °ÍÀº Kernel32.DLLÀÌ °¢°¢ÀÇ ÇÁ·Î¼¼½º °ø°£¿¡¼ Ç×»ó µ¿ÀÏÇÑ ÁÖ¼Ò·Î ¸ÅÇÎµÇ¾î ½ÇÇà ÁßÀÎ ¾î¶² ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡¼µµ LoadLibrary()
ÀÇ ÁÖ¼Ò°¡ ¶È°°´Ù´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº CreateRemoteThread()
ÇÔ¼öÀÇ ¸Å°³º¯¼ö·Î¼ À¯È¿ÇÑ Æ÷ÀÎÅÍ (i.e. pfnLoadLibrary
)¸¦ ³Ñ±æ ¼ö ÀÖ´Ù´Â È®½ÅÀ» ÁØ´Ù.
As parameter of the thread function we use the full path name of the DLL, casting it to LPVOID
. When the remote thread is resumed, it passes the name of the DLL to the ThreadFunction (i.e. LoadLibrary
). That's the whole trick with regard to using remote threads for injection purposes.
¾²·¹µå ÇÔ¼öÀÇ ¸Å°³º¯¼ö·Î DLLÀÇ Àüü °æ·Î¸íÀ» ÁöÁ¤ÇÏ°í LPVOID
·Î Çüº¯È¯ÇÑ´Ù. ¸®¸ðÆ® ¾²·¹µå°¡ ½ÃÀÛÇÒ ¶§ DLLÀÇ À̸§ÀÌ ¾²·¹µå ÇÔ¼ö(i.e. LoadLibrary
)ÀÇ ¸Å°³º¯¼ö·Î ³Ñ°ÜÁø´Ù. ÀÌ°ÍÀÌ ¸®¸ðÆ® ¾²·¹µå¸¦ »ç¿ëÇÏ¿© ħÅõ¸¦ ÇÏ´Â ¼ÓÀÓ¼öÀÇ ÀüºÎÀÌ´Ù.
There is an important thing we should consider, if implanting through CreateRemoteThread()
API. Every time before the injector application operate on the virtual memory of the targeted process and makes a call to CreateRemoteThread()
, it first opens the process using OpenProcess()
API and passes PROCESS_ALL_ACCESS
flag as parameter. This flag is used when we want to get maximum access rights to this process. In this scenario OpenProcess()
will return NULL
for some of the processes with low ID number. This error (although we use a valid process ID) is caused by not running under security context that has enough permissions. If you think for a moment about it, you will realize that it makes perfect sense. All those restricted processes are part of the operating system and a normal application shouldn't be allowed to operate on them. What would happen if some application has a bug and accidentally attempts to terminate an operating system's process? To prevent the operating system from that kind of eventual crashes, it is required that a given application must have sufficient privileges to execute APIs that might alter operating system behavior. To get access to the system resources (e.g. smss.exe, winlogon.exe, services.exe, etc) through OpenProcess()
invocation, you must be granted the debug privilege. This ability is extremely powerful and offers a way to access the system resources, that are normally restricted. Adjusting the process privileges is a trivial task and can be described with the following logical operations:
SeDebugPrivilege"
, we should locate its local LUID mapping. The privileges are specified by name and can be found in Platform SDK file winnt.h
SeDebugPrivilege"
privilege by calling AdjustTokenPrivileges()
API
OpenProcessToken()
process token handle CreateRemoteThread()
API¸¦ »ç¿ëÇÏ¿© ħÅõÇÏ´Â °æ¿ì¿¡´Â ¹Ýµå½Ã °í·ÁÇØ¾ß ÇÏ´Â Áß¿äÇÑ »çÇ×ÀÌ Çϳª ÀÖ´Ù. ħÅõ ¾îÇø®ÄÉÀ̼ÇÀÌ ¸ñÇ¥·Î ÇÏ´Â ÇÁ·Î¼¼½ºÀÇ °¡»ó ¸Þ¸ð¸®¿¡¼ ÀÛµ¿ÇÏ°í CreateRemoteThread()
¸¦ È£ÃâÇϱâ Àü¿¡ OpenProcess()
API¿¡ PROCESS_ALL_ACCESS
Ç÷¡±×¸¦ ¸Å°³º¯¼ö·Î ³Ñ°ÜÁÖ¾î ¸ÕÀú ÇÁ·Î¼¼½º¸¦ ¿ÀÇÂÇÏ¿©¾ß ÇÑ´Ù. ÀÌ Ç÷¡±×´Â ÇÁ·Î¼¼½º¿¡ ´ëÇÑ ÃÖ´ëÇÑÀÇ Á¢±Ù ±ÇÇÑÀ» ¾ò±â À§ÇØ »ç¿ëµÈ´Ù. ÀÌ °æ¿ì ÀϺΠÀÛÀº IDÀÇ ÇÁ·Î¼¼½º´Â OpenProcess()
ÇÔ¼ö°¡ NULL
À» ¹ÝȯÇÑ´Ù. À¯È¿ÇÑ ÇÁ·Î¼¼½º ID¸¦ »ç¿ëÇßÀ½¿¡µµ ÀÌ·¯ÇÑ ¿À·ù°¡ ³ª´Â °ÍÀº ÃæºÐÇÑ ±ÇÇÑÀ» °¡Áö´Â º¸¾È ·¹º§¿¡¼ ½ÇÇàµÇÁö ¾Ê¾Ò±â ¶§¹®ÀÌ´Ù. ÀÌ°Í¿¡ ´ëÇØ Àá½Ã »ìÆì º¸¸é Á¤È®ÇÑ Àǹ̸¦ ¾Ë ¼ö ÀÖ´Ù. ÀÌ·¸°Ô Á¦ÇѵǴ ¸ðµç ÇÁ·Î¼¼½º´Â ¿î¿µÃ¼Á¦ÀÇ ÀϺκÐÀÌ°í ÀϹÝÀûÀÎ ¾îÇø®ÄÉÀ̼ÇÀÌ ÀÌ·± ÇÁ·Î¼¼½º¸¦ Á¶ÀÛÇÏ´Â °ÍÀº Çã¿ëµÇÁö ¾Ê´Â´Ù. ¾î¶² ¾îÇø®ÄÉÀ̼ÇÀÌ ¹ö±×¸¦ °¡Áö°í ÀÖ¾î ¿î¿µÃ¼Á¦ÀÇ ÇÁ·Î¼¼½º¸¦ ÁßÁö½Ãų·Á°í ÇÑ´Ù¸é ¾î¶»°Ô µÇ°Ú´Â°¡? ¿î¿µÃ¼Á¦¸¦ ÀÌ·¯ÇÑ À¯ÇüÀÇ »ç°í·ÎºÎÅÍ º¸È£Çϱâ À§ÇØ ¾î¶² ¾îÇø®ÄÉÀ̼ÇÀÌ ¿î¿µÃ¼Á¦ÀÇ µ¿ÀÛÀ» º¯°æÇÒ ¼ö ÀÖ´Â APIµéÀ» ½ÇÇàÇϱâ À§Çؼ´Â ÃæºÐÇÑ ±ÇÇÑÀ» °¡Á®¾ß¸¸ °¡´ÉÇÏ´Ù. OpenProcess()
È£ÃâÀ» ÅëÇØ ½Ã½ºÅÛ ÀÚ¿ø(¿¹: smss.exe, winlogon.exe, services.exe..)¿¡ ´ëÇØ Á¢±ÙÇÏ·Á¸é µð¹ö±× ±ÇÇÑÀ» °¡Á®¾ß¸¸ ÇÑ´Ù. ÀÌ ±ÇÇÑÀº ¸Å¿ì °·ÂÇϸç ÀϹÝÀûÀ¸·Î Á¦ÇѵǴ ½Ã½ºÅÛ ÀÚ¿ø¿¡ ´ëÇÑ Á¢±ÙÀ» Çã¿ëÇÑ´Ù. ÇÁ·Î¼¼½ºÀÇ ±ÇÇÑÀ» Á¶Á¤ÇÏ´Â °ÍÀº »ç¼ÒÇÑ ÀÛ¾÷ÀÌ¸ç ¾Æ·¡¿¡ ³í¸®ÀûÀÎ ¹æ¹ýÀ» ¼³¸íÇÏ¿´´Ù.
SeDebugPrivilege
"À¸·Î LUID¸ÅÇÎÀ» ã´Â´Ù. ±ÇÇÑÀº À̸§À¸·Î ±¸ºÐµÇ¸ç Ç÷§Æû SDK ÆÄÀÏ winnt.h¿¡¼ ãÀ» ¼ö ÀÖ´Ù.
SeDebugPrivilege
" ±ÇÇÑÀ» È°¼ºÈÇϱâ À§ÇØ AdjustTokenPrivileges()
API¸¦ È£ÃâÇÏ¿© ÅäÅ«À» Á¶Á¤ÇÑ´Ù.
OpenProcessToken()
À¸·Î ¾òÀº ÇÁ·Î¼¼½º ÅäÅ« ÇÚµéÀ» ´Ý´Â´Ù. ±ÇÇÑ º¯°æ¿¡ °üÇÑ º¸´Ù ÀÚ¼¼ÇÑ ¼³¸íÀº [10] "Using privilege"¸¦ ÂüÁ¶ÇÑ´Ù.
IObjectWithSite
interface.¶§¶§·Î ÀÎÅÍ³Ý ÀͽºÇ÷η¯¿¡¸¸ »ç¿ëÀÚ Á¤ÀÇ Äڵ带 ħÅõ½ÃÅ°´Â °ÍÀÌ ÇÊ¿äÇÒ ¼ö ÀÖ´Ù. ´ÙÇེ·´°Ôµµ ¸¶ÀÌÅ©·Î¼ÒÇÁÆ®´Â ÀÌ·¯ÇÑ ¸ñÀû¿¡ ºÎÇÕÇÏ´Â ½±°í Àß ¹®¼ÈµÈ Brower Helper Object¶ó´Â ¹æ¹ýÀ» Á¦°øÇÑ´Ù. BHO´Â COM DLL·Î ±¸ÇöµÇ¸ç ÀûÀýÇÏ°Ô µî·ÏµÇ±â¸¸ Çϸé IE°¡ ½ÇÇàµÉ ¶§¸¶´Ù IE°¡ IObjectWithSite
ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÑ ¸ðµç COM °´Ã¼¸¦ ·ÎµåÇÑ´Ù.
BHO¿Í À¯»çÇÏ°Ô MS ¿ÀÇǽº¿¡ ÀÚ½ÅÀÇ Äڵ带 ħÅõ½ÃÅ°·Á¸é MS ¿ÀÇǽº ¾ÖµåÀÎÀ¸·Î ±¸ÇöµÇ´Â Ç¥ÁØ ¸ÞÄ«´ÏÁòÀÇ ÀåÁ¡À» ÀÌ¿ëÇÒ ¼ö ÀÖ´Ù. ÀÌ·¯ÇÑ Á¾·ùÀÇ ¾ÖµåÀÎÀ» ±¸ÇöÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÏ´Â ¿¹Á¦µéÀº ¸¹ÀÌ ÀÖ´Ù.
Injecting a DLL into the address space of an external process is a key element of a spying system. It provides an excellent opportunity to have a control over process's thread activities. However it is not sufficient to have the DLL injected if you want to intercept API function calls within the process.
´Ù¸¥ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡ DLLÀ» ħÅõ½ÃÅ°´Â °ÍÀº ½ºÆÄÀÌ ½Ã½ºÅÛÀÇ Áß¿äÇÑ ¿ä¼ÒÀÌ´Ù. ÀÌ°ÍÀº ÇÁ·Î¼¼½ºÀÇ ¾²·¹µå È°µ¿¿¡ ´ëÇØ Á¦¾îÇÒ ¼ö ÀÖ´Â ÈǸ¢ÇÑ ±âȸ¸¦ Á¦°øÇÑ´Ù. ±×·¯³ª ÇÁ·Î¼¼½º ³»ÀÇ API ÇÔ¼ö È£ÃâÀ» °¡·Îä·Á¸é DLLÀ» ħÅõ½ÃÅ°´Â °Í¸¸À¸·Î´Â ÃæºÐÄ¡ ¾Ê´Ù.
This part of the article intends to make a brief review of several available real-world hooking aspects. It focuses on the basic outline for each one of them, exposing their advantages and disadvantages.
Áö±ÝºÎÅÍ ½ÇÁ¦ ÈÄÅ·ÀÇ ¿©·¯°¡Áö ÇüÅ¿¡ ´ëÇØ °£´ÜÇÏ°Ô »ìÆì º¸°Ú´Ù. °¢°¢ÀÇ ±âº»ÀûÀÎ À±°û¿¡ ÃÊÁ¡À» ¸ÂÃß°í ÀåÁ¡°ú ´ÜÁ¡À» ¼³¸íÇÏ°Ú´Ù.
In terms of the level where the hook is applied, there are two mechanisms for API spying - Kernel level and User level spying. To get better understanding of these two levels you must be aware of the relationship between the Win32 subsystem API and the Native API. Following figure demonstrates where the different hooks are set and illustrates the module relationships and their dependencies on Windows 2K:
ÈÄÅ©°¡ Àû¿ëµÇ´Â ·¹º§ÀÇ Ãø¸é¿¡¼ º¸¸é API ÈÄÅ·Àº Ä¿³Î ·¹º§°ú »ç¿ëÀÚ ·¹º§ÀÇ 2°¡Áö ¸ÞÄ«´ÏÁòÀ¸·Î ±¸ºÐµÈ´Ù. ÀÌ 2°¡Áö ·¹º§¿¡ ´ëÇÑ ÀÌÇظ¦ µ½±â À§ÇØ ¼³¸íÇÏÀÚ¸é Win32 ¼ºê½Ã½ºÅÛ API¿Í ³×ÀÌƼºê APIÀÇ °ü°è¸¦ ¾Ë¾Æ¾ß¸¸ ÇÑ´Ù. ¾Æ·¡ÀÇ ±×¸²Àº ´Ù¸¥ Á¾·ùÀÇ ÈÄÅ©°¡ ¼³Ä¡µÇ´Â À§Ä¡¸¦ º¸¿©ÁÖ¸ç À©µµ¿ìÁî 2K¿¡¼ ¸ðµâ °£ÀÇ °ü°è¿Í ÀÇÁ¸¼ºÀ» Ç¥½ÃÇÏ°í ÀÖ´Ù.
Figure 2
The major implementation difference between them is that interceptor engine for kernel-level hooking is wrapped up as a kernel-mode driver, whereas user-level hooking usually employs user-mode DLL.
À̵éÀ» ±¸ÇöÇÔ¿¡ ÀÖ¾î °¡Àå Áß¿äÇÑ Â÷ÀÌÁ¡Àº Ä¿³Î ·¹º§ ÈÄÅ·ÀÇ °¡·Îä±â ¿£ÁøÀº Ä¿³Î ¸ðµå µå¶óÀ̹ö·Î Æ÷ÀåµÇÁö¸¸ »ç¿ëÀÚ ·¹º§ ÈÄÅ·Àº ÀϹÝÀûÀ¸·Î »ç¿ëÀÚ ¸ðµå DLLÀ» »ç¿ëÇÑ´Ù´Â °ÍÀÌ´Ù.
There are several methods for achieving hooking of NT system services in kernel mode. The most popular interception mechanism was originally demonstrated by Mark Russinovich and Bryce Cogswell in their article [3] "Windows NT System-Call Hooking". Their basic idea is to inject an interception mechanism for monitoring NT system calls just bellow the user mode. This technique is very powerful and provides an extremely flexible method for hooking the point that all user-mode threads pass through before they are serviced by the OS kernel.
Ä¿³Î ¸ðµå¿¡¼ NT ½Ã½ºÅÛ ¼ºñ½ºÀÇ ÈÄÅ·À» Çϱâ À§Çؼ´Â ¸î°¡Áö ¹æ¹ýµéÀÌ ÀÖ´Ù. °¡Àå ÀαâÀÖ´Â °¡·Îä±â ¸ÞÄ«´ÏÁòÀº Mark Russinovich¿Í Bryce CogswellÀÌ ±×µéÀÇ ±Û [3] "Windows NT System-Call Hooking"¿¡¼ óÀ½ ¼Ò°³ÇÏ¿´´Ù. ±×µéÀÇ ±âº» ¾ÆÀ̵ð¾î´Â NT ½Ã½ºÅÛ È£ÃâÀ» °¨½ÃÇϱâ À§ÇÑ °¡·Îä±â ¸ÞÄ«´ÏÁòÀ» ´ÜÁö »ç¿ëÀÚ ¸ðµå ÇϺο¡ ħÅõ½ÃÅ°´Â °ÍÀÌ´Ù.(???)
You can find an excellent design and implementation in "Undocumented Windows 2000 Secrets" as well. In his great book Sven Schreiber explains how to build a kernel-level hooking framework from scratch [5].
"Undocumented Windows 2000 Secrets" [5]¿¡¼µµ ¶Ù¾î³ ¼³°è¿Í ±¸ÇöÀ» ¹ß°ßÇÒ ¼ö ÀÖ´Ù. ÀÌ ÈǸ¢ÇÑ Ã¥¿¡¼ Sven Schreiber´Â ¾î¶»°Ô Ä¿³Î ·¹º§ ÈÄÅ· ÇÁ·¹ÀÓ¿÷À» ±¸ÃàÇϴ°¡¸¦ °³·«ÀûÀ¸·Î ¼³¸íÇÏ¿´´Ù.
Another comprehensive analysis and brilliant implementation has been provided by Prasad Dabak in his book "Undocumented Windows NT" [17].
¶Ç´Ù¸¥ Æø³ÐÀº ºÐ¼®°ú ¸í¼®ÇÑ ±¸ÇöÀ» Prasad DabakÀÇ Ã¥ "Undocumented Windows NT" [17]¿¡¼µµ ãÀ» ¼ö ÀÖ´Ù.
However, all these hooking strategies, remain out of the scope of this article.
ÇÏÁö¸¸ ÀÌ·¯ÇÑ ¸ðµç ÈÄÅ· Àü·«µéÀº ÀÌ ±ÛÀÇ ¹üÀ§¸¦ ¹þ¾î³´Ù.
SetWindowLongPtr()
GWLP_WNDPROC
parameter and pass the ÀÌ ¹æ¹ýÀº ¾îÇø®ÄÉÀ̼ÇÀÇ µ¿ÀÛÀÌ À©µµ¿ì ÇÁ·Î½ÃÀú¸¦ »õ·Ó°Ô ±¸ÇöÇÏ¿© º¯°æµÉ ¼ö ÀÖ´Â »óȲ¿¡ ÀûÇÕÇÏ´Ù. ÀÌ ÀÛ¾÷À» Çϱâ À§Çؼ´Â GWLP_WNDPROC
¸¦ ¸Å°³º¯¼ö·Î ÇÏ¿© SetWindowLongPtr()
ÇÔ¼ö¸¦ È£ÃâÇÏ°í »õ·Î¿î À©µµ¿ì ÇÁ·Î½ÃÀúÀÇ Æ÷ÀÎÅ͸¦ ³Ñ°Ü Áֱ⸸ ÇÏ¸é µÈ´Ù. ÀÏ´Ü »õ·Î¿î ¼ºêŬ·¡½º ÇÁ·Î½ÃÀú°¡ ¼³Á¤µÇ¸é À©µµ¿ìÁî´Â ¸Þ½ÃÁö¸¦ ƯÁ¤ À©µµ¿ì·Î º¸³¾ ¶§¸¶´Ù ƯÁ¤ À©µµ¿ì¿Í ¿¬°üµÈ À©µµ¿ì ÇÁ·Î½ÃÀúÀÇ ÁÖ¼Ò¸¦ ÆľÇÇؼ ±âÁ¸ÀÇ ÇÁ·Î½ÃÀú ´ë½Å »õ·Î¿î ÇÁ·Î½ÃÀú¸¦ È£ÃâÇÏ°Ô µÈ´Ù.
The drawback of this mechanism is that subclassing is available only within the boundaries of a specific process. In other words an application should not subclass a window class created by another process.
Usually this approach is applicable when you hook an application through add-in (i.e. DLL / In-Proc COM component) and you can obtain the handle to the window whose procedure you would like to replace.
For example, some time ago I wrote a simple add-in for IE (Browser Helper Object) that replaces the original pop-up menu provided by IE using subclassing.
ÀÌ ¸ÞÄ«´ÏÁòÀÇ ´ÜÁ¡Àº ¼ºêŬ·¡½ÌÀÌ ÇϳªÀÇ Æ¯Á¤ ÇÁ·Î¼¼½ºÀÇ ¿µ¿ªÀ¸·Î¸¸ Á¦Çѵȴٴ °ÍÀÌ´Ù. ¹Ù²ã ¸»Çϸé ÇϳªÀÇ ¾îÇø®ÄÉÀ̼ÇÀº ´Ù¸¥ ¾îÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ »ý¼ºµÈ À©µµ¿ì Ŭ·¡½º´Â ¼ºêŬ·¡½ÌÀ» ÇÒ ¼ö°¡ ¾ø´Ù.
ÀϹÝÀûÀ¸·Î ÀÌ·¯ÇÑ Á¢±Ù ¹æ¹ýÀº ¾ÖµåÀÎ(i.e. DLL / In-Proc COM component)À» ÅëÇØ ÈÄÅ·ÇÏ°í ±³Ã¼ÇÏ·Á´Â ÇÁ·Î½ÃÀúÀÇ À©µµ¿ì ÇÚµéÀ» ±¸ÇÒ ¼ö ÀÖ´Â °æ¿ì¿¡ À¯¿ëÇÏ´Ù.
¿¹¸¦ µé¸é, ¾ó¸¶ Àü¿¡ ³ª´Â IE°¡ »ç¿ëÇÏ´Â ¿ø·¡ÀÇ Æ˾÷ ¸Þ´º¸¦ ¼ºêŬ·¡½ÌÀ» ÀÌ¿ëÇÏ¿© ±³Ã¼ÇÏ´Â °£´ÜÇÑ IE ¾ÖµåÀÎ(BHO) ÀÛ¼ºÇÏ¿´´Ù.
API¸¦ ÇØÅ·ÇÏ´Â ½¬¿î ¹æ¹ýÀº °°Àº À̸§À» °¡Áö°í ¿øº»°ú µ¿ÀÏÇÑ ÀͽºÆ÷Æ® ½Éº¼À» °¡Áö´Â DLL·Î ¹Ù²ãÄ¡´Â °ÍÀÌ´Ù. ÀÌ ±â¼úÀº ÇÔ¼ö Æ÷¿ö´õ¸¦ ÀÌ¿ëÇÏ¿© ½±°Ô ±¸ÇöÇÒ ¼ö ÀÖ´Ù. ÇÔ¼ö Æ÷¿ö´õ´Â ±âº»ÀûÀ¸·Î ÇÔ¼ö¿¡ ´ëÇÑ È£ÃâÀ» ´Ù¸¥ DLLÀÇ ÇÔ¼ö·Î À§ÀÓÇÏ´Â DLL ÀͽºÆ÷Æ® ¼½¼ÇÀÇ ¿£Æ®¸®ÀÌ´Ù.
You can accomplish this task by simply using #pragma comment
:
ÀÌ ÀÛ¾÷Àº ´Ü¼øÈ÷ #pragma comment
À» ÀÌ¿ëÇÏ¿© ±¸ÇöÇÒ ¼ö ÀÖ´Ù:
#pragma comment(linker, "/export:DoSomething=DllImpl.ActuallyDoSomething")
However, if you decide to employ this method, you should take the responsibility of providing compatibilities with newer versions of the original library. For more details see [13a] section "Export forwarding" and [2] "Function Forwarders".
±×·¯³ª ÀÌ ¹æ¹ýÀ» »ç¿ëÇϱâ·Î ÇÑ´Ù¸é ¿øº» ¶óÀ̺귯¸®¿Í »õ·Î¿î ¹öÀüÀÌ È£È¯µÇµµ·Ï À¯ÁöÇÏ¿©¾ß ÇÑ´Ù. ÀÚ¼¼ÇÑ ³»¿ëÀº [13a] "Export forwarding" °ú [2] "Function Forwarders"À» ÂüÁ¶Çϱ⠹ٶõ´Ù.
ÄÚµå µ¤¾î¾²±â¿¡ ±âÃÊÇÑ ¸î°¡Áö ¹æ¹ýµéÀÌ ÀÖ´Ù. ±×Áß Çϳª´Â CALL ¸í·É¿¡ ÀÇÇØ »ç¿ëµÇ´Â ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ º¯°æÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¹æ¹ýÀº ¾î·Æ°í ¿À·ù¸¦ ¹ß»ý½ÃÅ°±â ½±´Ù. ±âº»ÀûÀÎ ¾ÆÀ̵ð¾î´Â ¸Þ¸ð¸® »óÀÇ ¸ðµç CALL ¸í·ÉÀ» ÃßÀûÇÏ°í ¿øº» ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ »ç¿ëÀÚ°¡ Á¤ÀÇÇÑ ÁÖ¼Ò·Î ¹Ù²Ù´Â °ÍÀÌ´Ù.
Another method of code overwriting requires a more complicated implementation. Briefly, the concept of this approach is to locate the address of the original API function and to change first few bytes of this function with a JMP instruction that redirects the call to the custom supplied API function. This method is extremely tricky and involves a sequence of restoring and hooking operations for each individual call. It's important to point out that if the function is in unhooked mode and another call is made during that stage, the system won't be able to capture that second call.
The major problem is that it contradicts with the rules of a multithreaded environment.
However, there is a smart solution that solves some of the issues and provides a sophisticated way for achieving most of the goals of an API interceptor. In case you are interested you might peek at [12] Detours implementation.
ÄÚµå µ¤¾î¾²±âÀÇ ¶Ç´Ù¸¥ ¹æ¹ýÀº º¸´Ù º¹ÀâÇÑ ±¸ÇöÀ» ÇÊ¿ä·Î ÇÑ´Ù. °£´ÜÇÏ°Ô ¼³¸íÇϸé ÀÌ Á¢±ÙÀÇ °³³äÀº ¿øº» API ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ÆľÇÇÏ°í ÀÌ ÇÔ¼öÀÇ Ã¹¹ø° ¸î ¹ÙÀÌÆ®¸¦ »ç¿ëÀÚ Á¤ÀÇ API ÇÔ¼ö·Î ¿¬°á½ÃÅ°´Â JMP ¸í·ÉÀ¸·Î º¯°æÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¹æ¹ýÀº ¸Å¿ì ±³¹¦ÇÏ°í °¢°¢ÀÇ °³º°ÀûÀΠȣÃâ¿¡ ´ëÇØ ÀÏ·ÃÀÇ º¹¿ø°ú ÈÄÅ·À» ¹Ýº¹ÇÏ°Ô ÇÑ´Ù. ¸¸ÀÏ ÇÔ¼ö°¡ ÈÄÅ©µÇÁö ¾ÊÀº »óÅ¿¡¼ ´Ù¸¥ È£ÃâÀÌ ¹ß»ýÇÏ¸é ½Ã½ºÅÛÀº µÎ¹ø° È£ÃâÀ» °¡·Îç ¼ö ¾ø´Ù´Â °ÍÀº ¸Å¿ì Áß¿äÇÏ´Ù.
°¡Àå Å« ¹®Á¦Á¡Àº ¸ÖƼ¾²·¹µå ȯ°æÀÇ ±ÔÄ¢¿¡ À§¹èµÈ´Ù´Â °ÍÀÌ´Ù.
ÇÏÁö¸¸ ÀÌ·¯ÇÑ ¸î°¡Áö ¹®Á¦Á¡µéÀ» ÇØ°áÇÏ°í API °¡·Îä±âÀÇ ´ëºÎºÐÀÇ ¸ñÀûÀ» ´Þ¼ºÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â ¼¼·ÃµÈ ¹æ¹ýÀÌ ÀÖ´Ù. ¸¸¾à °ü½ÉÀÌ ÀÖ´Ù¸é [12] Detours implementation¸¦ ÂüÁ¶Çϱ⠹ٶõ´Ù.
API ÇÔ¼ö¸¦ ÈÄÅ·ÇÏ´Â ¶Ç´Ù¸¥ ¹æ¹ýÀº µð¹ö±ë ¸ØÃãÁ¡À» ¸ñÇ¥·Î ÇÏ´Â ÇÔ¼ö¿¡ À§Ä¡½ÃÅ°´Â °ÍÀÌ´Ù. ÇÏÁö¸¸ ÀÌ ¹æ¹ý¿¡´Â ¸î°¡Áö ´ÜÁ¡µéÀÌ ÀÖ´Ù. ÀÌ ¹æ½ÄÀÇ °¡Àå Å« ¹®Á¦´Â µð¹ö±ë ¿¹¿Ü°¡ ¸ðµç ¾îÇø®ÄÉÀÌ¼Ç ¾²·¹µå¸¦ ´ë±â½ÃŲ´Ù´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº ¶ÇÇÑ ¿¹¿Ü¸¦ Á¶ÀÛÇÒ µð¹ö°Å ÇÁ·Î¼¼½º¸¦ ÇÊ¿ä·Î ÇÑ´Ù. ¶Ç´Ù¸¥ ¹®Á¦´Â µð¹ö°Å°¡ Á¾·áÇÒ ¶§ À©µµ¿ìÁî°¡ ÀÚµ¿À¸·Î µð¹ö°Å¸¦ ³¡³½´Ù´Â »ç½Ç¿¡ ±âÀÎÇÑ´Ù.(???)
ÀÌ ±â¼úÀº Matt PietrekÀÌ Ã³À½ ¹ßÇ¥ÇÏ¿´°í ±×ÈÄ¿¡ Jeffrey Ritcher ([2] "API Hooking by Manipulating a Module's Import Section") ¿Í John Robbins ([4] "Hooking Imported Functions")¿¡ ÀÇÇØ ´Ùµë¾îÁ³´Ù. ÀÌ ¹æ¹ý ¸Å¿ì °ß½ÇÇÏ¸ç ´Ü¼øÇÏ°í ±¸ÇöÇϱ⠻ó´çÈ÷ ½¬¿î ¹æ¹ýÀÌ´Ù. ¶ÇÇÑ NT/2k¿Í 9x ¿î¿µÃ¼Á¦¸¦ ¸ðµÎ Áö¿øÇÏ´Â ÈÄÅ· ÇÁ·¹ÀÓ¿÷ÀÇ ¿ä±¸»çÇ×ÀÇ ´ëºÎºÐÀ» ¸¸Á·½Ãų ¼ö ÀÖ´Ù. ÀÌ ±â¼úÀÇ °³³äÀº PE(Portable Executable) À©µµ¿ìÁî ÆÄÀÏ Æ÷¸ËÀÇ ¿ì¾ÆÇÑ ±¸Á¶¿¡ ÀÇÁ¸ÇÑ´Ù. ÀÌ ¹æ¹ýÀÌ ¾î¶»°Ô Àû¿ëµÇ´Â°¡¸¦ ÀÌÇØÇÏ·Á¸é Common Object File Format(COFF)ÀÇ È®Àå ÇüÅÂÀÎ PE ÆÄÀÏ Æ÷¸Ë¿¡ ´ëÇÑ ±âº» Áö½Ä¿¡ Ä£¼÷ÇØÁ®¾ß ÇÑ´Ù. Matt PietrekÀº ±×ÀÇ ¸ÚÁø ±ÛÀÎ [6] "Peering Inside the PE." ¿Í [13a/b] "An In-Depth Look into the Win32 PE file format"¿¡¼ PE Æ÷¸ËÀÇ º£ÀÏÀ» ¹þ°å´Ù. PE Ư¼ºÀÇ Àü¹ÝÀûÀÎ ¼³¸í¸¸À¸·Îµµ ÀÓÆ÷Æ® ÁÖ¼Ò Å×À̺íÀ» Á¶ÀÛÇÏ¿© ÈÄÅ·À» ±¸ÇöÇÏ´Â ¾ÆÀ̵ð¾î¸¦ ¾òÀ» ¼ö ÀÖ´Ù.
In general an PE binary file is organized, so that it has all code and data sections in a layout that conform to the virtual memory representation of an executable. PE file format is composed of several logical sections. Each of them maintains specific type of data and addresses particular needs of the OS loader.
ÀϹÝÀûÀ¸·Î PE ÀÌÁø ÆÄÀÏÀÌ »ý¼ºµÇ¸é ½ÇÇà½ÃÀÇ °¡»ó ¸Þ¸ð¸® ±¸Á¶¸¦ µû¸£´Â ÇüÅÂÀÇ ÄÚµå¿Í µ¥ÀÌÅÍ ¼½¼ÇÀ» °®°Ô µÈ´Ù. PE ÆÄÀÏ Æ÷¸ËÀº ¸î°¡Áö ³í¸®ÀûÀÎ ¼½¼ÇÀ¸·Î ±¸¼ºµÈ´Ù. ±×°Íµé °¢°¢Àº ƯÁ¤ À¯ÇüÀÇ µ¥ÀÌÅ͸¦ À¯ÁöÇÏ°í OS ·Î´õ¿¡°Ô Ưº°ÇÑ ¿ä±¸¸¦ Áö½ÃÇÑ´Ù.(???)
The section .idata
, I would like to focus your attention on, contains information about Import Address Table. This part of the PE structure is particularly very crucial for building a spy program based on altering IAT.
Each executable that conforms with PE format has layout roughly described by the figure below.
.idata
¼½¼ÇÀº Ưº°È÷ °ü½ÉÀ» ±â¿ï¿©¾ß Çϴµ¥ ÀÌ°ÍÀº ÀÓÆ÷Æ® ÁÖ¼Ò Å×À̺í(IAT)¿¡ °üÇÑ Á¤º¸¸¦ ´ã°í ÀÖ´Ù. PE ±¸Á¶ÀÇ ÀÌ ºÎºÐÀº IAT¸¦ º¯°æÀ» ±â¹ÝÀ¸·Î ÇÏ´Â ½ºÆÄÀÌ ÇÁ·Î±×·¥À» ÀÛ¼ºÇϴµ¥ ¸Å¿ì Áß¿äÇÏ´Ù.
¾Æ·¡ÀÇ ±×¸²Àº PE Æ÷¸ËÀÇ ½ÇÇà ÆÄÀÏ ±¸Á¶¸¦ °³·«ÀûÀ¸·Î ³ªÅ¸³»°í ÀÖ´Ù.
Figure 3
The program loader is responsible for loading an application along with all its linked DLLs into the memory. Since the address where each DLL is loaded into, cannot be known in advance, the loader is not able to determine the actual address of each imported function. The loader must perform some extra work to ensure that the program will call successfully each imported function. But going through each executable image in the memory and fixing up the addresses of all imported functions one by one would take unreasonable amount of processing time and cause huge performance degradation. So, how does the loader resolves this challenge? The key point is that each call to an imported function must be dispatched to the same address, where the function code resides into the memory. Each call to an imported function is in fact an indirect call, routed through IAT by an indirect JMP instruction. The benefit of this design is that the loader doesn't have to search through the whole image of the file. The solution appears to be quite simple - it just fixes-up the addresses of all imports inside the IAT. Here is an example of a snapshot PE File structure of a simple Win32 Application, taken with the help of the [8] PEView utility. As you can see TestApp import table contains two imported by GDI32.DLL function - TextOutA()
and GetStockObject()
.
ÇÁ·Î±×·¥ ·Î´õ´Â ¾îÇø®ÄÉÀ̼ÇÀ» ·ÎµåÇÏ¸é¼ ¾îÇø®ÄÉÀ̼ǿ¡ ¸µÅ©µÈ DLLµéÀ» ÇÔ²² ¸Þ¸ð¸®¿¡ ·ÎµåÇÑ´Ù. °¢°¢ÀÇ DLLÀÌ ·ÎµåµÇ´Â ÁÖ¼Ò´Â ¹Ì¸® ¾Ë ¼ö ¾ø±â ¶§¹®¿¡ ·Î´õ´Â ÀÓÆ÷Æ®µÈ °¢°¢ÀÇ ÇÔ¼öµéÀÇ ½ÇÁ¦ ÁÖ¼Ò¸¦ ¾ËÁö ¸øÇÑ´Ù. ·Î´õ´Â ÇÁ·Î±×·¥ÀÌ ÀÓÆ÷Æ®µÈ ÇÔ¼ö¸¦ ¼º°øÀûÀ¸·Î È£ÃâÇÒ ¼ö ÀÖµµ·Ï º°µµÀÇ ÀÛ¾÷À» ¼öÇàÇÏ¿©¾ß¸¸ ÇÑ´Ù. ÇÏÁö¸¸ ¸Þ¸ð¸® »óÀÇ ½ÇÇà À̹ÌÁö °¢°¢À» ÄmÀ¸¸é¼ ¸ðµç ÀÓÆ÷Æ®µÈ ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ ÇϳªÇϳª ¼öÁ¤ÇÏ´Â °ÍÀº °úµµÇÑ Ã³¸® ½Ã°£À» ¿ä±¸ÇÏ°í ¾öû³ ¼º´É ÀúÇϸ¦ À¯¹ßÇÑ´Ù. ±×·¸´Ù¸é ·Î´õ´Â ÀÌ·¯ÇÑ ¹®Á¦¸¦ ¾î¶»°Ô ÇØ°áÇÒ±î? Áß¿äÇÑ »ç½ÇÀº ÀÓÆ÷Æ®µÈ ÇÔ¼ö¿¡ ´ëÇÑ °¢°¢ÀÇ È£ÃâÀÌ ¸Þ¸ð¸® »ó¿¡¼ ÇÔ¼ö Äڵ尡 À§Ä¡ÇÏ´Â µ¿ÀÏÇÑ ÁÖ¼Ò·Î Àü´ÞµÇ¾î¾ß¸¸ ÇÑ´Ù´Â °ÍÀÌ´Ù. ÀÓÆ÷Æ®µÈ ÇÔ¼ö¿¡ ´ëÇÑ °¢°¢ÀÇ È£ÃâÀº »ç½Ç»ó IAT¸¦ °ÅÃÄ °£Á¢ JMP ¸í·ÉÀ» ÅëÇÏ´Â °£Á¢ÀûÀΠȣÃâÀÌ´Ù. ÀÌ·¯ÇÑ µðÀÚÀÎÀÇ ÀÌÁ¡Àº ·Î´õ°¡ ÆÄÀÏÀÇ ¸ðµç À̹ÌÁö¸¦ ÄmÁö ¾Ê¾Æµµ µÈ´Ù´Â °ÍÀÌ´Ù. ÇØ°áÃ¥ÀÌ ¾à°£ ´Ü¼øÇØ º¸ÀδÙ. ´ÜÁö IAT ³»ºÎÀÇ ¸ðµç ÀÓÆ÷Æ® ÁÖ¼Ò¸¦ ¼öÁ¤Çϱ⸸ ÇÏ´Â °ÍÀÌ´Ù. ¾Æ·¡¿¡ °£´ÜÇÑ Win32 ¾îÇø®ÄÉÀ̼ÇÀÇ PE ÆÄÀÏ ±¸Á¶ÀÇ ÇüŸ¦ [8] PEView utility¸¦ »ç¿ëÇÏ¿© º¸¿©ÁÖ´Â ¿¹°¡ ÀÖ´Ù. TestApp ÀÓÆ÷Æ® Å×À̺íÀÌ GDI32.DLLÀÇ 2°³ ÇÔ¼ö, TextOutA()
¿Í GetStockObject()
¸¦ Æ÷ÇÔÇÏ´Â °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù.
Figure 4
IMAGE_IMPORT_DESCRIPTOR
chunk of the DLL that exports that function. Practically speaking, usually we search this entry by the name of the DLL
IMAGE_THUNK_DATA
which holds the original address of the imported function
ÀÓÆ÷Æ®µÈ ÇÔ¼ö¸¦ ÈÄÅ·ÇÏ´Â °úÁ¤Àº óÀ½ º¸¾ÒÀ» ¶§ ´À³¢´Â °Íó·³ º¹ÀâÇÏÁö ¾Ê´Ù. °£´ÜÈ÷ ¸»Çϸé IAT¸¦ ¼öÁ¤ÇÏ´Â ÈÄÅ· ½Ã½ºÅÛÀº ÀÓÆ÷Æ®µÈ ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ °¡Áö°í ÀÖ´Â À§Ä¡¸¦ ã¾Æ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼öÀÇ ÁÖ¼Ò·Î µ¤¾î½á¼ ¹Ù²ã ÁÖ´Â °ÍÀÌ´Ù. ÀÌ °úÁ¤¿¡¼ Áß¿äÇÑ ¿ä±¸ »çÇ×Àº »õ·Î Á¦°øÇÏ´Â ÇÔ¼ö°¡ ±âÁ¸ÀÇ ÇÔ¼ö¿Í µ¿ÀÏÇÑ ÇüÅÂÀ̾î¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù. ¾Æ·¡¿¡ ±³Ã¼ ½ÎÀÌŬÀÇ ³í¸®Àû ´Ü°è¸¦ ¼³¸íÇÏ¿´´Ù.
IMAGE_IMPORT_DESCRIPTOR
ûũ¸¦ ã´Â´Ù. ½ÇÁ¦·Î´Â ÀÌ ¿£Æ®¸®¸¦ DLLÀÇ À̸§À¸·Î ã´Â´Ù.
IMAGE_THUNK_DATA
¸¦ ã´Â´Ù.
IAT ³»ºÎÀÇ ÀÓÆ÷Æ®µÈ ÇÔ¼öÀÇ ÁÖ¼Ò¸¦ º¯°æÇÔÀ¸·Î¼ ÈÄÅ·µÈ ÇÔ¼ö¿¡ ´ëÇÑ È£ÃâÀº »õ·Î¿î ÇÔ¼ö·Î ¿¬°áµÇ°Ô µÈ´Ù.
Replacing the pointer inside the IAT is that .idata
section doesn't necessarily have to be a writable section. This requires that we must ensure that .idata
section can be modified. This task can be accomplished by using VirtualProtect()
API.
IAT ³»ºÎÀÇ Æ÷ÀÎÅ͸¦ ¹Ù²Ù±â À§Çؼ .idata
¼½¼ÇÀÌ ¹Ýµå½Ã ¾²±â °¡´ÉÇÒ ÇÊ¿ä´Â ¾ø´Ù. .idata
¼½¼ÇÀÌ ¼öÁ¤ °¡´ÉÇÏ´Ù´Â °ÍÀ» È®½ÅÇÏ´Â °ÍÀÌ ÇÊ¿äÇÏ´Ù. ÀÌ ÀÛ¾÷Àº VirtualProtect()
API¸¦ »ç¿ëÇÏ¿© ¼öÇàÇÒ ¼ö ÀÖ´Ù.
Another issue that deserves attention is related to the GetProcAddress()
API behavior on Windows 9x system. When an application calls this API outside the debugger it returns a pointer to the function. However if you call this function within from the debugger it actually returns different address than it would when the call is made outside the debugger. It is caused by the fact that that inside the debugger each call to GetProcAddress()
returns a wrapper to the real pointer. Returned by GetProcAddress()
value points to PUSH
instruction followed by the actual address. This means that on Windows 9x when we loop through the thunks, we must check whether the address of examined function is a PUSH
instruction (0x68 on x86 platforms) and accordingly get the proper value of the address function.
°ü½ÉÀ» °¡Á®¾ß ÇÏ´Â ¶Ç´Ù¸¥ ¹®Á¦Á¡Àº À©µµ¿ìÁî9x¿¡¼ GetProcAddress()
APIÀÇ µ¿ÀÛ°ú °ü·ÃÀÌ ÀÖ´Ù. ¾îÇø®ÄÉÀ̼ÇÀÌ ÀÌ API¸¦ µð¹ö°Å ¿ÜºÎ¿¡¼ È£ÃâÇϸé ÀÌ API´Â ÇÔ¼ö¿¡ ´ëÇÑ Æ÷ÀÎÅ͸¦ ¹ÝȯÇÑ´Ù. ÇÏÁö¸¸ µð¹ö°Å ³»¿¡¼ ÀÌ ÇÔ¼ö¸¦ È£ÃâÇÏ¸é µð¹ö°Å ¿ÜºÎ¿¡¼ È£ÃâÇÒ ¶§ ¹ÝȯÇÏ´Â ÁÖ¼Ò¿Í ´Ù¸¥ °ªÀ» ¹ÝȯÇÑ´Ù. ÀÌ·¯ÇÑ Çö»óÀº µð¹ö°Å ³»¿¡¼ÀÇ GetProcAddress()
È£ÃâÀÌ ½ÇÁ¦ Æ÷ÀÎÅ͸¦ º¯È¯(wrapper)Çؼ ¹ÝȯÇϱ⠶§¹®¿¡ ¹ß»ýÇÑ´Ù. GetProcAddress()
°¡ ¹ÝȯÇÏ´Â ÁÖ¼Ò°ªÀº PUSH
¸í·É°ú ½ÇÁ¦ ÁÖ¼Ò°¡ ³ª¿À´Â À§Ä¡¸¦ °¡¸£Å²´Ù. ÀÌ°ÍÀº À©µµ¿ìÁî 9x¿¡¼´Â ûũ¸¦ ¼øȸÇÒ ¶§ ¹Ýµå½Ã °Ë»çÇÑ ÇÔ¼öÀÇ ÁÖ¼Ò°¡ PUSH
¸í·É (0x68 on x86 platforms)Àΰ¡¸¦ üũÇÏ¿© ÁÖ¼Ò ÇÔ¼öÀÇ ÀûÇÕÇÑ °ªÀ» ¾ò¾î¾ß ÇÑ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù.
Windows 9x doesn't implement copy-on-write, thus operating system attempts to keep away the debuggers from stepping into functions above the 2-GB frontier. That is the reason why GetProcAddress()
returns a debug thunk instead of the actual address. John Robbins discusses this problem in [4] "Hooking Imported Functions".
À©µµ¿ìÁî 9x´Â copy-on-write¸¦ Áö¿øÇÏÁö ¾ÊÀ¸¹Ç·Î ¿î¿µÃ¼Á¦´Â µð¹ö°Å°¡ 2-GB ÇÑ°è »ó´ÜÀÇ ÇÔ¼ö¿¡ Á¢±ÙÇÏÁö ¸øÇϵµ·Ï ¸¸µç´Ù.
GetProcAddress()
°¡ ½ÇÁ¦ ÁÖ¼Ò ´ë½Å µð¹ö±× ûũ¸¦ ¹ÝȯÇÏ´Â ÀÌÀ¯ÀÌ´Ù. John Robbins´Â ÀÌ ¹®Á¦¿¡ ´ëÇÏ¿© [4] "Hooking Imported Functions".¿¡¼ ´Ù·ç¾ú´Ù. [¿ªÀÚÁÖ] ¿©±â±îÁöÀÇ ³»¿ëÀ» ¿ä¾àÇØ º¸¸é ´ÙÀ½°ú °°½À´Ï´Ù.
That section reveals some challenges that are faced by developers when the selected injection mechanism is not part of the operating system's functionality. For example, performing the injection is not your concern when you use built-in Windows Hooks in order to implant a DLL. It is an OS's responsibility to force each of those running processes that meet the requirements for this particular hook, to load the DLL [18]. In fact Windows keeps track of all newly launched processes and forces them to load the hook DLL. Managing injection through registry is quite similar to Windows Hooks. The biggest advantage of all those "built-in" methods is that they come as part of the OS.
Áö±Ý±îÁöÀÇ ¼½¼Ç¿¡¼ ¼±ÅÃÇÑ Ä§Åõ ¸ÞÄ«´ÏÁòÀÌ ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â ±â´ÉÀÌ ¾Æ´Ñ °æ¿ì °³¹ßÀÚµéÀÌ Á÷¸éÇÏ°Ô µÇ´Â ¸î°¡Áö ¹®Á¦Á¡µé¿¡ ´ëÇØ ¼³¸íÇÏ¿´´Ù. ¿¹¸¦ µé¸é, DLLÀ» ÁÖÀÔ½ÃÅ°±â À§ÇØ ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â À©µµ¿ìÁî ÈÄÅ©¸¦ »ç¿ëÇÑ´Ù¸é ħÅõ¸¦ ¼öÇàÇÏ´Â °ÍÀº °ü½ÉÀÇ ´ë»óÀÌ ¾Æ´Ï´Ù. ƯÁ¤ ÈÄÅ©ÀÇ Á¶°Ç¿¡ ºÎÇÕÇÏ´Â ½ÇÇà ÇÁ·Î¼¼½º°¡ DLLÀ» ·Îµå[18]Çϵµ·Ï ÇÏ´Â °ÍÀº ¿î¿µÃ¼Á¦ÀÇ ¸òÀÌ´Ù. »ç½Ç»ó À©µµ¿ìÁî »õ·Î ÀûÀçµÈ ¸ðµç ÇÁ·Î¼¼½º¸¦ ÃßÀûÇÏ°í ÇÁ·Î¼¼½ºµéÀÌ ÈÄÅ· DLLÀ» ·ÎµåÇϵµ·Ï °Á¦ÇÑ´Ù. ·¹Áö½ºÆ®¸®¸¦ ÅëÇÑ Ä§Åõ °ü¸®´Â À©µµ¿ìÁî ÈÄÅ©¿Í ¾à°£ À¯»çÇÏ´Ù. ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â ¸ðµç ¹æ¹ýµéÀÇ °¡Àå Å« ÀåÁ¡Àº ¿î¿µÃ¼Á¦ÀÇ ÀϺκÐÀ¸·Î¼ Á¦°øµÈ´Ù´Â °ÍÀÌ´Ù.
Unlike the discussed above implanting techniques, to inject by CreateRemoteThread()
requires maintenance of all currently running processes. If the injecting is made not on time, this can cause the Hook System to miss some of the calls it claims as intercepted. It is crucial that the Hook Server application implements a smart mechanism for receiving notifications each time when a new process starts or shuts down. One of the suggested methods in this case, is to intercept CreateProcess()
API family functions and monitor all their invocations. Thus when an user supplied function is called, it can call the original CreateProcess()
with dwCreationFlags
OR
-ed with CREATE_SUSPENDED
flag. This means that the primary thread of the targeted application will be in suspended state, and the Hook Server will have the opportunity to inject the DLL by hand-coded machine instructions and resume the application using ResumeThread() API. For more details you might refer to [2] "Injecting Code with CreateProcess()"
.
»ó±âÀÇ Ä§Åõ ±â¼ú°ú ´Þ¸® CreateRemoteThread()
¸¦ ÀÌ¿ëÇϴ ħÅõ´Â ÇöÀç ½ÇÇà ÁßÀÎ ¸ðµç ÇÁ·Î¼¼½º¿¡ ´ëÇÑ °ü¸®¸¦ ¿ä±¸ÇÑ´Ù. ¸¸ÀÏ Ä§Åõ°¡ ÀûÀýÇÑ ¶§¿¡ ÀÌ·ç¾îÁöÁö ¾Ê´Â´Ù¸é ÈÄÅ© ½Ã½ºÅÛÀÌ °¡·Îä±â·Î ÁöÁ¤ÇÑ È£ÃâÀÇ ÀϺθ¦ ¼öÇàÇÏÁö ¸øÇÒ ¼ö°¡ ÀÖ´Ù. ÈÄÅ· ¼¹ö ¾îÇø®ÄÉÀ̼ÇÀÌ »õ·Î¿î ÇÁ·Î¼¼½º°¡ ½ÃÀ۵ǰųª Á¾·áÇÒ ¶§¸¶´Ù Å뺸¸¦ ¹ÞÀ» ¼ö ÀÖµµ·Ï ¼¼·ÃµÈ ¸ÞÄ«´ÏÁòÀ» ±¸ÇöÇÏ´Â °ÍÀº ¸Å¿ì Áß¿äÇÏ´Ù. ÀÌ·¯ÇÑ °æ¿ì Á¦¾ÈµÇ´Â ¹æ¹ý ÁßÀÇ Çϳª°¡ CreateProcess()
API Æйи®ÀÇ ÇÔ¼ö¸¦ °¡·Îä¼ °¨½ÃÇÏ´Â °ÍÀÌ´Ù. ±×¸®°í ÈÄÅ·µÈ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö°¡ È£ÃâµÉ ¶§ ¿øº» CreateProcess()
ÇÔ¼ö¸¦ dwCreationFlags
¿¡ CREATE_SUSPENDED
Ç÷¡±×¸¦ OR ¿¬»êÇÏ¿© È£ÃâÇÑ´Ù. ÀÌ°ÍÀº ¸ñÇ¥·Î ÇÏ´Â ¾îÇø®ÄÉÀ̼ÇÀÇ ÇÁ¶óÀ̸Ӹ® ¾²·¹µå¸¦ ´ë±â »óÅ·ΠÇÏ¿© ÈÄÅ© ¼¹ö°¡ DLLÀ» ħÅõ½ÃÅ°µµ·Ï ÇÏ°í ResumeThread()
API·Î ¾îÇø®ÄÉÀ̼ÇÀ» ±âµ¿½ÃÅ°´Â °ÍÀ» ÀǹÌÇÑ´Ù. º¸´Ù ÀÚ¼¼ÇÑ Á¤º¸¸¦ ¿øÇÑ´Ù¸é ·¹ÆÛ·±½ºÀÇ [2] "Injecting Code with CreateProcess()
"¸¦ ÂüÁ¶Ç϶ó.
The second method of detecting process execution, is based on implementing a simple device driver. It offers the greatest flexibility and deserves even more attention. Windows NT/2K provides a special function PsSetCreateProcessNotifyRoutine()
exported by NTOSKRNL. This function allows adding a callback function, that is called whenever a process is created or deleted. For more details see [11] and [15] from the reference section.
ÇÁ·Î¼¼½ºÀÇ ½ÇÇàÀ» °¨ÁöÇÏ´Â µÎ¹ø° ¹æ¹ýÀº °£´ÜÇÑ µð¹ÙÀ̽º µå¶óÀ̹ö¸¦ ±¸ÇöÇÏ´Â °ÍÀÌ´Ù. ÀÌ ¹æ¹ýÀº °¡Àå À¯¿¬ÇÏ°í Å« °ü½ÉÀ» °¡Áú ¸¸ÇÏ´Ù. À©µµ¿ìÁî NT/2K´Â NTOKKRNL¿¡¼ ÀͽºÆ÷Æ®µÈ PsSetCreateProcessNotifyRoutine()
¶ó´Â Ưº°ÇÑ ÇÔ¼ö¸¦ Á¦°øÇÑ´Ù. ÀÌ ÇÔ¼ö´Â ÇÁ·Î¼¼½ºÀÇ »ý¼ºÀ̳ª ¼Ò¸ê ½Ã¿¡ È£ÃâµÇ´Â Äݹé ÇÔ¼ö¸¦ Ãß°¡Çϵµ·Ï ÇØÁØ´Ù. º¸´Ù ÀÚ¼¼ÇÑ ³»¿ëÀº ·¹ÆÛ·±½ºÀÇ [11]°ú [15]¸¦ ÂüÁ¶Ç϶ó.
Sometimes we would prefer to use injecting of the DLL by CreateRemoteThread()
API, especially when the system runs under NT/2K. In this case when the Hook Server is started it must enumerate all active processes and inject the DLL into their address spaces. Windows 9x and Windows 2K provide a built-in implementation (i.e. implemented by Kernel32.dll) of Tool Help Library. On the other hand Windows NT uses for the same purpose PSAPI library. We need a way to allow the Hook Server to run and then to detect dynamically which process "helper" is available. Thus the system can determine which the supported library is, and accordingly to use the appropriate APIs.
¶§¶§·Î CreateRemoteThread()
API¸¦ ÀÌ¿ëÇÏ¿© DLLÀ» ħÅõ½ÃÅ°´Â ¹æ¹ýÀÌ ¼±È£µÈ´Ù. ƯÈ÷ ½Ã½ºÅÛÀÌ NT/2KÀÎ °æ¿ì¿¡ ±×·¯ÇÏ´Ù. ÀÌ °æ¿ì, ÈÄÅ© ¼¹ö°¡ ½ÃÀÛµÉ ¶§ ¸ðµç È°¼º ÇÁ·Î¼¼½º¸¦ ³ª¿ÇÏ°í °¢°¢ÀÇ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡ DLLÀ» ħÅõ½ÃÄÑ¾ß ÇÑ´Ù. À©µµ¿ìÁî 9x¿Í 2K´Â Tool Help LibraryÀÇ ³»Àå ±¸Çö(Kernel32.dll·Î ±¸ÇöµÈ)À» Á¦°øÇÑ´Ù. À©µµ¿ìÁî NT¿¡¼´Â PSAPI ¶óÀ̺귯¸®¸¦ °°Àº ¸ñÀûÀ¸·Î »ç¿ëÇÒ ¼ö ÀÖ´Ù. ±×·¯¹Ç·Î ÈÄÅ© ¼¹ö´Â ½ÇÇàµÈ ÈÄ ¾î¶² ÇÁ·Î¼¼½º ÇïÆÛ ¶óÀ̺귯¸®¸¦ »ç¿ëÇÒ ¼ö ÀÖÁö¸¦ ÆÇ´ÜÇÏ¿© ÀûÀýÇÑ APIµéÀ» »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ¸¸µé¾îÁ®¾ß ÇÑ´Ù.
I will present an object-oriented architecture that implements a simple framework for retrieving processes and modules under NT/2K and 9x [16]. The design of my classes allows extending the framework according to your specific needs. The implementation itself is pretty straightforward.
ÀÌÁ¦ºÎÅÍ NT/2K¿Í 9x [16] ȯ°æ¿¡¼ ÇÁ·Î¼¼½º¿Í ¸ðµâÀ» ÃßÃâÇÏ´Â °£´ÜÇÑ ÇÁ·¹ÀÓ¿÷¿¡ ´ëÇÑ °´Ã¼ ÁöÇâ ±¸Á¶¿¡ ´ëÇØ ¼³¸íÇÏ°Ú´Ù. ÀÌ Å¬·¡½ºµéÀÇ µðÀÚÀÎÀº ƯÁ¤ ¿ä±¸ »çÇ׿¡ ¸ÂÃç È®ÀåÀÌ ¿ëÇϵµ·Ï µÇ¾îÀÖ´Ù. ±¸Çö ÀÚü´Â ¸Å¿ì ¼ö¿ùÇÏ´Ù.
CTaskManager
implements the system's processor. It is responsible for creating an instance of a specific library handler (i.e. CPsapiHandler
or CToolhelpHandler
) that is able to employ the correct process information provider library (i.e. PSAPI or ToolHelp32 respectively). CTaskManager
is in charge of creating and marinating a container object that keeps a list with all currently active processes. After instantiating of the CTaskManager
object the application calls Populate()
method. It forces enumerating of all processes and DLL libraries and storing them into a hierarchy kept by CTaskManager
's member m_pProcesses
.
Following UML diagram shows the class relationships of this subsystem:
CTaskManager
´Â ½Ã½ºÅÛÀÇ ÇÁ·Î¼¼¼¸¦ ´ã´çÇÑ´Ù. ÀÌ Å¬·¡½º´Â ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â ¶óÀ̺귯¸®((i.e. PSAPI or ToolHelp32 respectively)¸¦ Ãë»ç¼±ÅÃÇÏ¿© ƯÁ¤ ¶óÀ̺귯¸® Çڵ鷯(CPsapiHandler
or CToolhelpHandler
)ÀÇ ÀνºÅϽº¸¦ »ý¼ºÇÏ´Â ¿ªÇÒÀ» ÇÑ´Ù. CTaskManager
´Â ÇöÀç È°¼ºÈµÈ ¸ðµç ÇÁ·Î¼¼½ºÀÇ ¸®½ºÆ®¸¦ À¯ÁöÇÏ´Â ÄÁÅ×ÀÌ³Ê °´Ã¼ÀÇ »ý¼ºÀ» °ü¸®ÇÑ´Ù. CTaskManager
°´Ã¼°¡ »ý¼ºµÈ ÈÄ¿¡´Â ¾îÇø®ÄÉÀ̼ÇÀº Populate()
¸Þ¼Òµå¸¦ È£ÃâÇÒ ¼ö ÀÖ´Ù. ÀÌ ¸Þ¼Òµå´Â ¸ðµç ÇÁ·Î¼¼½º¿Í DLL ¶óÀ̺귯¸®¸¦ ³ª¿ÇÏ°í ±× Á¤º¸¸¦ CTaskManager
ÀÇ ¸â¹ö º¯¼öÀÎ m_pProcesses
¿¡ °èÃþÀûÀ¸·Î ÀúÀåÇÑ´Ù.
¾Æ·¡ÀÇ UML ´ÙÀ̾î±×·¥Àº ÀÌ·¯ÇÑ ¼ºê ½Ã½ºÅÛÀÇ Å¬·¡½º °ü°è¸¦ º¸¿©ÁØ´Ù.
Figure 5
It is important to highlight the fact that NT's Kernel32.dll doesn't implement any of the ToolHelp32 functions. Therefore we must link them explicitly, using runtime dynamic linking. If we use static linking the code will fail to load on NT, regardless whether or not the application has attempted to execute any of those functions. For more details see my article "Single interface for enumerating processes and modules under NT and Win9x/2K.".
NTÀÇ Kernel32.dllÀÌ ToolHelp32 ÇÔ¼öÀÇ ¾î¶² °Íµµ ±¸ÇöÇÏÁö ¾Ê´Â´Ù´Â °ÍÀº ¸Å¿ì Áß¿äÇÑ »ç½ÇÀÌ´Ù. ±×·¯¹Ç·Î ½ÇÇà½Ã µ¿Àû ¸µÅ©¸¦ ÀÌ¿ëÇÏ¿© ¸í½ÃÀûÀ¸·Î DLLµéÀ» ¸µÅ©ÇÏ¿©¾ß ÇÑ´Ù. ¸¸ÀÏ Á¤Àû ¸µÅ©¸¦ »ç¿ëÇÑ´Ù¸é ¾îÇø®ÄÉÀ̼ÇÀÌ ±× ÇÔ¼öµéÀ» »ç¿ëÇϴ°¡¿¡ ¹«°üÇÏ°Ô ±× ÄÚµå´Â NT¿¡¼ ·Îµå¿¡ ½ÇÆÐÇÒ °ÍÀÌ´Ù. º¸´Ù ÀÚ¼¼ÇÑ ³»¿ëÀº "Single interface for enumerating processes and modules under NT and Win9x/2K."À» ÂüÁ¶Çϱ⠹ٶõ´Ù.
Now that I've made a brief introduction to the various concepts of the hooking process it's time to determine the basic requirements and explore the design of a particular hooking system. These are some of the issues addressed by the Hook Tool System:
Áö±Ý±îÁö ÈÄÅ· ÇÁ·Î¼¼½ºÀÇ ´Ù¾çÇÑ °³³äµéÀ» °£·«ÇÏ°Ô ¼³¸íÇÏ¿´´Ù. ÀÌÁ¦ºÎÅÍ´Â ±âº» ¿ä±¸ »çÇ×À» °áÁ¤ÇÏ°í ÈÄÅ· ½Ã½ºÅÛÀ» ¼³°èÇÏ´Â °ÍÀ» ¿¬±¸ÇØ º¸°Ú´Ù. ´ÙÀ½ÀÇ »çÇ×µéÀº ÈÄÅ© Åø ½Ã½ºÅÛ¿¡¼ Á¦±âµÇ´Â À̽´µéÀÌ´Ù:
CreateRemoteThread()
API. The framework should offer an ability to set this up by an INI file
TextOutA/W()
and ExitProcess()
API functions
CreateRemoteThread()
API»Ó ¾Æ´Ï¶ó À©µµ¿ìÁî ÈÄÅ©¸¦ »ç¿ëÇÏ¿© ¸ðµç ½ÇÇà ÇÁ·Î¼¼½º¿¡ ÈÄÅ© µå¶óÀ̹ö¸¦ ħÅõ½Ãų ¼ö ÀÖµµ·Ï ÇÑ´Ù. ÇÁ·ºÀÓ¿÷Àº ÀÌ°ÍÀ» INI ÆÄÀÏ·Î ¼³Á¤ÇÒ ¼ö ÀÖ¾î¾ß ÇÑ´Ù.
TextOutA/W()
¿Í ExitProcess()
API ÇÔ¼ö¿¡ ´ëÇÑ »ç¿ëÀÚ Á¤ÀÇ ¹öÀüÀ» ±¸ÇöÇÑ´Ù.
This part of the article discusses the key components of the framework and how do they interact each other. This outfit is capable to capture any kind of WINAPI
imported by name functions.
ÀÌ ¼½¼Ç¿¡¼´Â ÇÁ·¹ÀÓ¿÷ÀÇ ÁÖ¿ä ÄÄÆ÷³ÍÆ®¿Í ¼·Î °£ÀÇ »óÈ£ ÀÛ¿ë¿¡ ´ëÇØ »ìÆì º¸°Ú´Ù. ÀÌ ½Ã½ºÅÛÀº À̸§À¸·Î ÀÓÆ÷Æ®µÇ´Â ¾î¶°ÇÑ Á¾·ùÀÇ WINAPI
ÇÔ¼öµµ °¡·Îç ¼ö°¡ ÀÖ´Ù.
Before I outline the system's design, I would like to focus your attention on several methods for injecting and hooking.
½Ã½ºÅÛÀÇ ¼³°è¸¦ ¿äÁ¡À» ¼³¸íÇϱâ Àü¿¡ ħÅõ¿Í ÈÄÅ·ÀÇ ¿©·¯ ¹æ¹ýµé¿¡ °ü½ÉÀ» ±â¿ïÀ̱⠹ٶõ´Ù.
First and foremost, it is necessary to select an implanting method that will meet the requirements for injecting the DLL driver into all processes. So I designed an abstract approach with two injecting techniques, each of them applied accordingly to the settings in the INI file and the type of the operating system (i.e. NT/2K or 9x). They are - System-wide Windows Hooks and CreateRemoteThread()
method. The sample framework offers the ability to inject the DLL on NT/2K by Windows Hooks as well as to implant by CreateRemoteThread()
means. This can be determined by an option in the INI file that holds all settings of the system.
¹«¾ùº¸´Ù ¸ÕÀú DLL µå¶óÀ̹ö¸¦ ¸ðµç ÇÁ·Î¼¼½º¿¡ ħÅõ½ÃÅ°´Â ¿ä±¸ »çÇ×À» ÃæÁ·ÇÏ´Â ÁÖÀÔ ¹æ¹ýÀ» ¼±ÅÃÇÏ´Â °ÍÀÌ ÇÊ¿äÇÏ´Ù. ±×·¡¼ INI ÆÄÀÏÀÇ ¼³Á¤°ú ¿î¿µÃ¼Á¦(i.e. NT/2K or 9x)ÀÇ Á¾·ù¿¡ µû¶ó Àû¿ëÇÒ 2°³ÀÇ Ä§Åõ ±â¼úÀ» °¡Áö°í Ãß»óÀûÀÎ Á¢±ÙÀ» ¼³°èÇß´Ù. ±×°ÍÀº ½Ã½ºÅÛ Àü¿ª À©µµ¿ìÁî ÈÄÅ©¿Í CreateRemoteThread()
ÀÌ´Ù. ¿¹Á¦ ÇÁ·¹ÀÓ¿÷Àº CreateRemoteThread()
ÇÔ¼ö¸¦ »ç¿ëÇؼ ÁÖÀÔÇÒ ¼ö ÀÖÀ» »Ó ¾Æ´Ï¶ó NT/2K¿¡¼ À©µµ¿ìÁî ÈÄÅ©¸¦ »ç¿ëÇÏ¿© DLLÀ» ħÅõ½Ãų ¼öµµ ÀÖ´Â ´É·ÂÀ» Á¦°øÇÑ´Ù. ¾î¶°ÇÑ °ÍÀ» »ç¿ëÇÒ °ÍÀΰ¡´Â ½Ã½ºÅÛÀÇ ¸ðµç ¼³Á¤À» °¡Áö°í ÀÖ´Â INI ÆÄÀÏÀÇ ¼³Á¤¿¡ µû¶ó °áÁ¤µÈ´Ù.
Another crucial moment is the choice of the hooking mechanism. Not surprisingly, I decided to apply altering IAT as an extremely robust method for Win32 API spying.
¶Ç´Ù¸¥ Áß¿äÇÑ °áÁ¤Àº ÈÄÅ· ¸ÞÄ«´ÏÁòÀ» ¼±ÅÃÇÏ´Â °ÍÀÌ´Ù. ´ç¿¬È÷ Win32 API¸¦ ÈÄÅ·ÇÏ´Â ¸Å¿ì °ß½ÇÇÑ ¹æ¹ýÀÎ IAT º¯°æÀ» Àû¿ëÇϱâ·Î °áÁ¤ÇÏ¿´´Ù.
To achieve desired goals I designed a simple framework composed of the following components and files:
¿ä±¸µÇ´Â ¸ñÇ¥¸¦ ´Þ¼ºÇϱâ À§ÇØ ´ÙÀ½ÀÇ ÄÞÆ÷³ÍÆ®¿Í ÆÄÀÏ·Î ±¸¼ºµÇ´Â ´Ü¼øÇÑ ÇÁ·¹ÀÓ¿÷À» ¼³°èÇÏ¿´´Ù:
HookSrv is a simple control program. Its main role is to load the HookTool.DLL and then to activate the spying engine. After loading the DLL, the Hook Server calls InstallHook()
function and passes a handle to a hidden windows where the DLL should post all messages to.
HookSrv´Â ´Ü¼øÇÑ Á¦¾î ÇÁ·Î±×·¥ÀÌ´Ù. ÀÌ°ÍÀÇ ÁÖ¿ä ÀÓ¹«´Â HookTool.DLLÀ» ·ÎµåÇÏ¿© ½ºÆÄÀÌ ¿£ÁøÀ» È°¼ºÈ½ÃÅ°´Â °ÍÀÌ´Ù. DLLÀ» ·ÎµåÇÑ ÈÄ¿¡ ÈÄÅ© ¼¹ö´Â InstallHook()
ÇÔ¼ö¸¦ È£ÃâÇÏ°í DLLÀÌ ¸ðµç ¸Þ½ÃÁö¸¦ Àü´ÞÇØ¾ß ÇÏ´Â ¼û°ÜÁø À©µµ¿ì¿¡ ÇÚµéÀ» ³Ñ°ÜÁØ´Ù.
HookTool.DLL is the hook driver and the heart of presented spying system. It implements the actual interceptor and provides three user supplied functions TextOutA/W()
and ExitProcess()
functions.
HookTool.DLLÀº ÈÄÅ© µå¶óÀ̹öÀÌ°í ½ºÆÄÀÌ ½Ã½ºÅÛÀÇ ÇÙ½ÉÀÌ´Ù. ÀÌ°ÍÀº ½ÇÁúÀûÀÎ °¡·Îä±â¸¦ ±¸ÇöÇÏ°í TextOutA/W()
°ú ExitProcess()
¿¡ ´ëÇÑ 3°³ÀÇ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö¸¦ Á¦°øÇÑ´Ù.
Although the article emphasizes on Windows internals and there is no need for it to be object-oriented, I decided to encapsulate related activities in reusable C++ classes. This approach provides more flexibility and enables the system to be extended. It also benefits developers with the ability to use individual classes outside this project.
ÀÌ ±Û¿¡¼ À©µµ¿ìÁî ³»ºÎÀûÀÎ Ãø¸é¿¡ ´ëÇØ °Á¶ÇÏ¿´°í ¹Ýµå½Ã ½Ã½ºÅÛÀÌ °´Ã¼ ÁöÇâÀ̾î¾ß ÇÒ ÀÌÀ¯´Â ¾øÁö¸¸ »óÈ£ °£ÀÇ µ¿ÀÛÀ» Àç»ç¿ë °¡´ÉÇÑ C++ Ŭ·¡½º¿¡ ĸ½¶ÈÇϱâ·Î °áÁ¤ÇÏ¿´´Ù. ÀÌ·¯ÇÑ Á¢±ÙÀº ´õ¸¹Àº À¯¿¬¼ºÀ» Á¦°øÇÏ°í ½Ã½ºÅÛÀÌ È®Àå °¡´ÉÇϵµ·Ï ¸¸µé¾î ÁØ´Ù. ¶ÇÇÑ °³¹ßÀÚ°¡ ´Ù¸¥ ÇÁ·ÎÁ§Æ®¿¡¼µµ °³º°ÀûÀΠŬ·¡½º¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù´Â ÀåÁ¡ÀÌ ÀÖ´Ù.
Following UML class diagram illustrates the relationships between set of classes used in HookTool.DLL's implementation.
¾Æ·¡ÀÇ UML ´ÙÀ̾î±×·¥Àº HookTool.DLLÀÇ ±¸Çö¿¡ »ç¿ëµÇ´Â ÀÏ·ÃÀÇ Å¬·¡½ºµéÀÇ °ü°è¸¦ ³ªÅ¸³½´Ù.
Figure 6
In this section of the article I would like to draw your attention to the class design of the HookTool.DLL. Assigning responsibilities to the classes is an important part of the development process. Each of the presented classes wraps up a specific functionality and represents a particular logical entity.
ÀÌ ¼½¼Ç¿¡¼´Â HookTool.DLLÀÇ Å¬·¡½º ¼³°è¿¡ °ü½ÉÀ» °¡Áö±â ¹Ù¶õ´Ù. Ŭ·¡½º¿¡ ¿ªÇÒÀ» ÇÒ´çÇÏ´Â °ÍÀº °³¹ß °úÁ¤¿¡¼ ¸Å¿ì Áß¿äÇÑ ºÎºÐÀÌ´Ù. Á¦½ÃµÈ Ŭ·¡½ºµé °¢°¢Àº Ưº°ÇÑ ±â´ÉÀ» °¨Ãß°í ÀÖ°í Ưº°ÇÑ ³í¸®Àû °³Ã¼·Î Ç¥ÇöµÈ´Ù.
CModuleScope
is the main doorway of the system. It is implemented using "Singleton" pattern and works in a thread-safe manner. Its constructor accepts 3 pointers to the data declared in the shared segment, that will be used by all processes. By this means the values of those system-wide variables can be maintained very easily inside the class, keeping the rule for encapsulation.
CModuleScope
´Â ½Ã½ºÅÛÀÇ ÁÖ ÃâÀÔ±¸ÀÌ´Ù. ÀÌ Å¬·¡½º´Â ½Ì±ÛÅÏ ÆÐÅÏÀ» »ç¿ëÇÏ¿© ±¸ÇöµÇ¾ú°í thread-safeÇÑ ¹æ½ÄÀ¸·Î µ¿ÀÛÇÑ´Ù. ÀÌ Å¬·¡½ºÀÇ »ý¼ºÀÚ´Â °øÀ¯ ¼¼±×¸ÕÆ®¿¡ ¼±¾ðµÇ¾î ÀÖ´Â 3°³ÀÇ µ¥ÀÌÅÍ¿¡ ´ëÇÑ Æ÷ÀÎÅ͸¦ ¸Å°³º¯¼ö·Î ³Ñ°Ü¹Þ°í ÀÌ µ¥ÀÌÅÍ´Â ¸ðµç ÇÁ·Î¼¼½º¿¡¼ »ç¿ëÇÏ°Ô µÈ´Ù. ÀÌ°ÍÀº ½Ã½ºÅÛ Àü¿ª º¯¼öÀÇ °ªÀÌ Å¬·¡½º ³»ºÎ¿¡¼ ¸Å¿ì ½±°Ô °ü¸®µÈ´Ù´Â °ÍÀ» ÀǹÌÇÏ°í ĸ½¶ÈÀÇ ±ÔÄ¢À» À¯ÁöÇÏ°Ô µÈ´Ù.
When an application loads the HookTool library, the DLL creates one instance of CModuleScope
on receiving DLL_PROCESS_ATTACH
notification. This step just initializes the only instance of CModuleScope
. An important piece of the CModuleScope
object construction is the creation of an appropriate injector object. The decision which injector to use will be made after parsing the HookTool.ini file and determining the value of UseWindowsHook
parameter under [Scope] section. In case that the system is running under Windows 9x, the value of this parameter won't be examined by the system, because Window 9x doesn't support injecting by remote threads.
¾î¶² ¾îÇø®ÄÉÀ̼ÇÀÌ HookTool ¶óÀ̺귯¸®¸¦ ·ÎµåÇÒ ¶§ DLLÀº DLL_PROCESS_ATTACH
ÅëÁö¸¦ ¹Þ°í CModuleScope
°´Ã¼¸¦ Çϳª »ý¼ºÇÑ´Ù. CModuleScope
°´Ã¼ »ý¼ºÀÇ Áß¿äÇÑ ºÎºÐÀº ÀûÇÕÇÑ Ä§Åõ °´Ã¼¸¦ »ý¼ºÇÏ´Â °ÍÀÌ´Ù. ¾î¶² ħÅõ Ŭ·¡½º¸¦ »ç¿ëÇÒ °ÍÀΰ¡´Â HookTool.ini ÆÄÀÏÀ» Àоî [Scope] ¼½¼ÇÀÇ UseWindowsHook
Ç׸ñÀÇ °ªÀ» È®ÀÎÇÑ ÈÄ¿¡ °áÁ¤µÈ´Ù. ½Ã½ºÅÛÀÌ À©µµ¿ìÁî 9x¿¡¼ ½ÇÇàµÇ´Â °æ¿ì¿¡´Â ¸®¸ðÆ® ¾²·¹µå¸¦ »ç¿ëÇϴ ħÅõ°¡ Áö¿øµÇÁö ¾ÊÀ¸¹Ç·Î ÀÌ Ç׸ñÀº ½Ã½ºÅÛÀÌ ¹«½ÃÇÑ´Ù.
After instantiating of the main processor object, a call to ManageModuleEnlistment()
method will be made. Here is a simplified version of its implementation:
ÁÖ Ã³¸® °´Ã¼°¡ »ý¼ºµÈ ÈÄ¿¡ ManageModuleEnlistment()
¸Þ¼Òµå¸¦ È£ÃâÇÏ°Ô µÈ´Ù. ¾Æ·¡¿¡ ÀÌ ¸Þ¼ÒµåÀÇ ±¸ÇöÀ» ´Ü¼øÈÇÑ ¼Ò½º°¡ ÀÖ´Ù.
// Called on DLL_PROCESS_ATTACH DLL notification BOOL CModuleScope::ManageModuleEnlistment() { BOOL bResult = FALSE; // Check if it is the hook server if (FALSE == *m_pbHookInstalled) { // Set the flag, thus we will know that the server has been installed *m_pbHookInstalled = TRUE; // and return success error code bResult = TRUE; } // and any other process should be examined whether it should be // hooked up by the DLL else { bResult = m_pInjector->IsProcessForHooking(m_szProcessName); if (bResult) InitializeHookManagement(); } return bResult; }
The implementation of the method ManageModuleEnlistment()
is straightforward and examines whether the call has been made by the Hook Server, inspecting the value m_pbHookInstalled
points to. If an invocation has been initiated by the Hook Server, it just sets up indirectly the flag sg_bHookInstalled
to TRUE. It tells that the Hook Server has been started.
ManageModuleEnlistment()
¸Þ¼ÒµåÀÇ ±¸ÇöÀº °£´ÜÇÏ´Ù. m_pbHookInstalled
°¡ °¡¸£Å°´Â °ªÀ» °Ë»çÇÏ¿© ÈÄÅ© ¼¹ö¿¡ ÀÇÇÑ È£ÃâÀΰ¡¸¦ È®ÀÎÇÑ´Ù. ¸¸ÀÏ ÈÄÅ© ¼¹ö¿¡ ÀÇÇÑ ½ÇÇàÀ̶ó¸é ´Ü¼øÈ÷ sg_bHookInstalled
¸¦ TRUE·Î ¼³Á¤ÇÑ´Ù. ÀÌ°ÍÀº ÈÄÅ© ¼¹ö°¡ ÀÌ¹Ì ½ÃÀ۵ǾúÀ½À» ³ªÅ¸³½´Ù.
The next action taken by the Hook Server is to activate the engine through a single call to InstallHook()
DLL exported function. Actually its call is delegated to a method of CModuleScope
- InstallHookMethod()
. The main purpose of this function is to force targeted for hooking processes to load or unload the HookTool.DLL.
ÈÄÅ© ¼¹ö°¡ ÃëÇÏ´Â ´ÙÀ½ ÇൿÀº DLLÀÇ ÀͽºÆ÷Æ®µÈ ÇÔ¼öÀÎ InstallHook()
¸¦ Çѹø È£ÃâÇÏ¿© ¿£ÁøÀ» È°¼ºÈ½ÃÅ°´Â °ÍÀÌ´Ù.
// Activate/Deactivate hooking engine BOOL CModuleScope::InstallHookMethod(BOOL bActivate, HWND hWndServer) { BOOL bResult; if (bActivate) { *m_phwndServer = hWndServer; bResult = m_pInjector->InjectModuleIntoAllProcesses(); } else { m_pInjector->EjectModuleFromAllProcesses(); *m_phwndServer = NULL; bResult = TRUE; } return bResult; }
HookTool.DLL provides two mechanisms for self injecting into the address space of an external process - one that uses Windows Hooks and another that employs injecting of DLL by CreateRemoteThread()
API. The architecture of the system defines an abstract class CInjector
that exposes pure virtual functions for injecting and ejecting DLL. The classes CWinHookInjector
and CRemThreadInjector
inherit from the same base - CInjector
class. However they provide different realization of the pure virtual methods InjectModuleIntoAllProcesses()
and EjectModuleFromAllProcesses()
, defined in CInjector
interface.
HookTool.DLLÀº ´Ù¸¥ ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡ ½º½º·Î ħÅõÇÏ´Â 2°¡ÁöÀÇ ¸ÞÄ«´ÏÁò -À©µµ¿ìÁî ÈÄÅ©¸¦ ÀÌ¿ëÇÏ´Â ÇÏ´Â ¹æ¹ý°ú CreateRemoteThread()
API¸¦ ÀÌ¿ëÇÏ¿© DLLÀ» ħÅõ½ÃÅ°´Â ¹æ¹ý- À» Á¦°øÇÑ´Ù. ½Ã½ºÅÛÀÇ ±¸Á¶´Â DLLÀ» ÁÖÀÔÇÏ°í »Ì¾Æ³»´Â ¼ø¼ö °¡»ó ÇÔ¼ö¸¦ °¡Áö´Â Ãß»ó Ŭ·¡½º CInjector
¸¦ Á¤ÀÇÇÑ´Ù. CWinHookInjector
¿Í CRemThreadInjector
´Â °°Àº ºÎ¸ð Ŭ·¡½º CInjector
¸¦ »ó¼ÓÇÑ´Ù. ÇÏÁö¸¸ À̵é ÀÚ½Ä Å¬·¡½ºµéÀº CInjector
ÀÎÅÍÆäÀ̽º¿¡ Á¤ÀÇµÈ ¼ø¼ö °¡»ó ¸Þ¼ÒµåÀÎ CWinHookInjector
¿Í CRemThreadInjector
¸¦ ´Ù¸¥ ¹æ½ÄÀ¸·Î ±¸ÇöÇÑ´Ù.
CWinHookInjector
class implements Windows Hooks injecting mechanism. It installs a filter function by the following call
CWinHookInjector
Ŭ·¡½º´Â À©µµ¿ìÁî ÈÄÅ©¸¦ ÀÌ¿ëÇϴ ħÅõ ¸ÞÄ«´ÏÁòÀ¸·Î ±¸ÇöµÈ´Ù. ÀÌ Å¬·¡½º´Â ¾Æ·¡ÀÇ ¼Ò½º¿Í °°ÀÌ ÇÊÅÍ ÇÔ¼ö¸¦ ¼³Ä¡ÇÑ´Ù.
// Inject the DLL into all running processes BOOL CWinHookInjector::InjectModuleIntoAllProcesses() { *sm_pHook = ::SetWindowsHookEx( WH_GETMESSAGE, (HOOKPROC)(GetMsgProc), ModuleFromAddress(GetMsgProc), 0 ); return (NULL != *sm_pHook); }
As you can see it makes a request to the system for registering WH_GETMESSAGE
hook. The server executes this method only once. The last parameter of SetWindowsHookEx()
is 0, because GetMsgProc()
is designed to operate as a system-wide hook. The callback function will be invoked by the system each time when a window is about to process a particular message. It is interesting that we have to provide a nearly dummy implementation of the GetMsgProc()
callback, since we don't intend to monitor the message processing. We supply this implementation only in order to get free injection mechanism provided by the operating system.
º¸´Â ¹Ù¿Í °°ÀÌ ÀÌ ¼Ò½º´Â ½Ã½ºÅÛ¿¡ WH_GETMESSAGE
ÈÄÅ©¸¦ µî·ÏÇϵµ·Ï ¿äûÇÑ´Ù. ¼¹ö´Â ÀÌ ¸Þ¼Òµå¸¦ ´ÜÇѹø ½ÇÇàÇÑ´Ù. GetMsgProc()
´Â ½Ã½ºÅÛ Àü¿ª ÈÄÅ©·Î µ¿ÀÛÇϵµ·Ï ¼³°èµÇ¾úÀ¸¹Ç·Î SetWindowsHookEx()
ÇÔ¼öÀÇ ¸¶Áö¸· ¸Å°³º¯¼ö´Â 0ÀÌ´Ù. Äݹé ÇÔ¼ö´Â À©µµ¿ì°¡ Ưº°ÇÑ ¸Þ½ÃÁö¸¦ ó¸®ÇÏ·Á°í ÇÒ ¶§ ¸¶´Ù ½ÇÇàµÈ´Ù. ¸Þ½ÃÁö ó¸® °úÁ¤À» °¨½ÃÇÏ´Â °ÍÀ» ÀǵµÇÏÁö ¾Ê´Â´Ù¸é GetMsgProc()
Äݹé ÇÔ¼ö¸¦ °ÅÀÇ ¾Æ¹« °Íµµ ÇÏÁö ¾Ê°Ô ±¸ÇöÇÏ¿© Á¦°øÇÏ¿©¾ß ÇÑ´Ù´Â °ÍÀº Èï¹Ì·Î¿î ÀÏÀÌ´Ù. ¿î¿µÃ¼Á¦°¡ Á¦°øÇÏ´Â ½¬¿î ħÅõ ¸ÞÄ«´ÏÁòÀ» ÀÌ¿ëÇϱâ À§Çؼ´Â ÀÌ·¸°Ô¸¸ ±¸ÇöÇÏ¸é µÈ´Ù.
After making the call to SetWindowsHookEx()
, OS checks whether the DLL (i.e. HookTool.DLL) that exports GetMsgProc()
has been already mapped in all GUI processes. If the DLL hasn't been loaded yet, Windows forces those GUI processes to map it. An interesting fact is, that a system-wide hook DLL should not return FALSE
in its DllMain()
. That's because the operating system validates DllMain()
's return value and keeps trying to load this DLL until its DllMain()
finally returns TRUE
.
SetWindowsHookEx()
ÇÔ¼ö¸¦ È£ÃâÇϸé OS´Â GetMsgProc()
¸¦ ÀͽºÆ÷Æ®ÇÏ°í ÀÖ´Â DLL(i.e. HookTool.DLL)ÀÌ ¸ðµç GUI ÇÁ·Î¼¼½º¿¡ ÀÌ¹Ì ¸ÅÇÎÀÌ µÇ¾î Àִ°¡¸¦ °Ë»çÇÑ´Ù. DLLÀÌ ¾ÆÁ÷ ·ÎµåµÇÁö ¾Ê¾Ò´Ù¸é À©µµ¿ìÁî´Â GUI ÇÁ·Î¼¼½º°¡ DLLÀ» ¸ÅÇÎÇϵµ·Ï ¸í·ÉÇÑ´Ù. Èï¹Ì·Î¿î »ç½ÇÀº ½Ã½ºÅÛ Àü¿ª ÈÄÅ© DLLÀº DllMain()
¿¡¼ Àý´ë·Î FALSE
¸¦ ¹ÝȯÇÏÁö ¾Ê´Â´Ù´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº ¿î¿µÃ¼Á¦°¡ DllMain()
ÀÇ ¹Ýȯ°ªÀ» °Ë»çÇÏ¿© DllMain()
ÀÌ TRUE
¸¦ ¹ÝȯÇÒ ¶§±îÁö ·Îµå¸¦ ½ÃµµÇϱ⠶§¹®ÀÌ´Ù.
A quite different approach is demonstrated by the CRemThreadInjector
class. Here the implementation is based on injecting the DLL using remote threads. CRemThreadInjector
extends the maintenance of the Windows processes by providing means for receiving notifications of process creation and termination. It holds an instance of CNtInjectorThread
class that observes the process execution. CNtInjectorThread
object takes care for getting notifications from the kernel-mode driver. Thus each time when a process is created a call to CNtInjectorThread ::OnCreateProcess()
is issued, accordingly when the process exits CNtInjectorThread ::OnTerminateProcess()
is automatically called. Unlike the Windows Hooks, the method that relies on remote thread, requires manual injection each time when a new process is created. Monitoring process activities will provide us with a simple technique for alerting when a new process starts.
CRemThreadInjector
Ŭ·¡½º´Â ¾ÆÁÖ ´Ù¸¥ ¹æ½ÄÀ¸·Î Á¢±ÙÇÑ´Ù. ÀÌÁ¦ºÎÅÍ´Â ¸®¸ðÆ® ¾²·¹µå¸¦ »ç¿ëÇÏ¿© DLLÀ» ħÅõ½ÃÅ°´Â ¹æ½Ä¿¡ ±âÃÊÇÑ ±¸ÇöÀ» ¼³¸íÇÏ°Ú´Ù. CRemThreadInjector
´Â ÇÁ·Î¼¼½ºÀÇ »ý¼º°ú ¼Ò¸ê¿¡ °üÇÑ ÅëÁö¸¦ ¹Þ´Â ¹æ¹ýÀ» ÀÌ¿ëÇÏ¿© À©µµ¿ìÁî ÇÁ·Î¼¼½ºÀÇ °ü¸®¸¦ È®Àå½ÃŲ´Ù. ÀÌ Å¬·¡½º´Â ÇÁ·Î¼¼½ºÀÇ ½ÇÇàÀ» °¨½ÃÇÏ´Â CNtInjectorThread
Ŭ·¡½º °´Ã¼¸¦ ¸â¹ö º¯¼ö·Î °¡Áø´Ù. CNtInjectorThread
°´Ã¼´Â Ä¿³Î ¸ðµå µå¶óÀ̹ö·ÎºÎÅÍ ÅëÁö¸¦ ¹Þ´Â °ÍÀ» °¨½ÃÇÑ´Ù. ¾î¶² ÇÁ·Î¼¼½º°¡ »ý¼ºµÉ ¶§ ¸¶´Ù CNtInjectorThread ::OnCreateProcess()
ÇÔ¼ö°¡ È£ÃâµÇ°í ÇÁ·Î¼¼½º°¡ Á¾·áÇÒ ¶§ CNtInjectorThread ::OnTerminateProcess()
ÇÔ¼ö°¡ È£ÃâµÈ´Ù. À©µµ¿ìÁî ÈÄÅ©¿Í ´Ù¸£°Ô ¸®¸ðÆ® ¾²·¹µå¿¡ ÀÇÁ¸ÇÏ´Â ¹æ½ÄÀº »õ·Î¿î ÇÁ·Î¼¼½º°¡ »ý¼ºµÉ ¶§ ¸¶´Ù ħÅõ ÀÛ¾÷ÀÌ ÇÊ¿äÇÏ´Ù. ÇÁ·Î¼¼½ºÀÇ È°µ¿À» °¨½ÃÇÏ´Â °ÍÀº »õ·Î¿î ÇÁ·Î¼¼½º°¡ ½ÃÀÛµÉ ¶§ ¸¶´Ù º¯°æ ÀÛ¾÷À» ÇÏ´Â °£´ÜÇÑ ¹æ¹ýÀ» Á¦°øÇÑ´Ù.
CNtDriverController
class implements a wrapper around API functions for administering services and drivers. It is designed to handle the loading and unloading of the kernel-mode driver NTProcDrv.sys. Its implementation will be discussed later.
CNtDriverController
Ŭ·¡½º´Â ¼ºñ½º¿Í µå¶óÀ̹ö¸¦ °ü¸®ÇÏ´Â API ÇÔ¼ö·Î ±¸ÇöµÈ´Ù. ÀÌ Å¬·¡½º´Â Ä¿³Î ¸ðµå µå¶óÀ̹ö NTProcDrv.sysÀÇ ·Îµå¿Í ¾ð·Îµå¸¦ Á¶ÀÛÇϵµ·Ï ¼³°èµÇ¾ú´Ù. ÀÌ°ÍÀÇ ±¸ÇöÀº ³ªÁß¿¡ ³íÀÇÇÏ°Ú´Ù.
After a successful injection of HookTool.DLL into a particular process, a call to ManageModuleEnlistment()
method is issued inside the DllMain()
. Recall the method's implementation that I described earlier. It examines the shared variable sg_bHookInstalled
through the CModuleScope
's member m_pbHookInstalled
. Since the server's initialization had already set the value of sg_bHookInstalled
to TRUE
, the system checks whether this application must be hooked up and if so, it actually activates the spy engine for this particular process.
¾î¶² ƯÁ¤ ÇÁ·Î¼¼½º·Î HookTool.DLLÀ» ħÅõ½ÃÅ°´Â °ÍÀÌ ¼º°øÇϸé DllMain()
¿¡¼ ManageModuleEnlistment()
ÇÔ¼ö¸¦ È£ÃâÇÏ°Ô µÈ´Ù. À§¿¡¼ ¼³¸íÇÑ ÀÌ ¸Þ¼ÒµåÀÇ ±¸ÇöÀ» »ý°¢ÇØ º¸ÀÚ. ÀÌ ÇÔ¼ö´Â CModuleScope
ÀÇ ¸â¹ö º¯¼öÀÎ m_pbHookInstalled
·Î ÀúÀåµÇ´Â °øÀ¯ÇÏ´Â º¯¼öÀÎ sg_bHookInstalled
¸¦ °Ë»çÇÑ´Ù. ¼¹öÀÇ ÃʱâÈ¿¡¼ ÀÌ¹Ì sg_bHookInstalled
ÀÇ °ªÀ» TRUE
·Î ¼³Á¤ÇÏ¿´À¸¹Ç·Î ½Ã½ºÅÛÀº ÀÌ ¾îÇø®ÄÉÀ̼ÇÀÌ ÈÄÅ©µÇ¾ú´Â°¡¸¦ °Ë»çÇÏ°í ±×·¸´Ù¸é ÀÌ ÇÁ·Î¼¼½º¿¡ ½ºÆÄÀÌ ¿£ÁøÀ» ½ÇÁúÀûÀ¸·Î È°¼ºÈ½ÃŲ´Ù.
Turning the hacking engine on, takes place in the CModuleScope::InitializeHookManagement()
's implementation. The idea of this method is to install hooks for some vital functions as LoadLibrary()
API family as well as GetProcAddress()
. By this means we can monitor loading of DLLs after the initialization process. Each time when a new DLL is about to be mapped it is necessary to fix-up its import table, thus we ensure that the system won't miss any call to the captured function.
ÈÄÅ· ¿£ÁøÀÌ È°¼ºÈµÇ¾úÀ¸¸é CModuleScope::InitializeHookManagement()
ÀÇ ±¸ÇöÀÌ ½ÇÇàµÈ´Ù. ÀÌ ¹æ½Ä¿¡¼´Â GetProcAddress()
¿Í LoadLibrary()
API °è¿ÀÇ ÇÔ¼ö¿¡ ÈÄÅ©¸¦ ¼³Ä¡ÇÑ´Ù. ÀÌ°ÍÀº ÃʱâÈ °úÁ¤ ÈÄ¿¡ DLLÀÇ ·Îµå¸¦ °¨½ÃÇÒ ¼ö ÀÖ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. ¾î¶² »õ·Î¿î DLLÀÌ ¸ÅÇ뵃 ¶§ ¸¶´Ù ±×°ÍÀÇ ÀÓÆ÷Æ® Å×À̺íÀ» ¼öÁ¤ÇÏ´Â ÀÛ¾÷ÀÌ ÇÊ¿äÇÏ°í ±×·¸°Ô ÇÔÀ¸·Î¼ ½Ã½ºÅÛÀº °¡·Îæ ÇÔ¼öÀÇ È£ÃâÀ» ³õÄ¡Áö ¾Ê°Ô µÈ´Ù.
At the end of the InitializeHookManagement()
method we provide initializations for the function we actually want to spy on.
InitializeHookManagement()
¸Þ¼ÒµåÀÇ ³¡ºÎºÐ¿¡¼ ½ÇÁ¦·Î ½ºÆÄÀÌÇϱ⸦ ¿øÇÏ´Â ÇÔ¼öÀÇ Ãʱâȸ¦ ÇÏ°Ô µÈ´Ù.
Since the sample code demonstrates capturing of more than one user supplied functions, we must provide a single implementation for each individual hooked function. This means that using this approach you cannot just change the addresses inside IAT of the different imported functions to point to a single "generic" interception function. The spying function needs to know which function this call comes to. It is also crucial that the signature of the interception routine must be exactly the same as the original WINAPI
function prototype, otherwise the stack will be corrupted. For example CModuleScope
implements three static methods MyTextOutA(),MyTextOutW() and MyExitProcess()
. Once the HookTool.DLL is loaded into the address space of a process and the spying engine is activated, each time when a call to the original TextOutA()
is issued, CModuleScope:: MyTextOutA()
gets called instead.
¿¹Á¦ Äڵ尡 ¿©·¯ °³ÀÇ »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö·Î °¡·Îä´Â °ÍÀ» º¸¿©ÁֹǷΠÈÄÅ© ÇÔ¼ö °¢°¢À» ó¸®ÇÒ ¼ö ÀÖ´Â ÇϳªÀÇ °øÅëµÈ ÇÔ¼ö·Î ±¸ÇöÇÏ¿©¾ß ÇÑ´Ù. ÀÌ°ÍÀº ÀÌ·¯ÇÑ ¹æ½ÄÀ» »ç¿ëÇؼ´Â ¼·Î ´Ù¸¥ ÀÓÆ÷Æ®µÈ ÇÔ¼öÀÇ IAT ³»ºÎÀÇ ÁÖ¼ÒµéÀ» ÇϳªÀÇ °¡·Îä±â ÇÔ¼ö¸¦ °¡¸®Å°µµ·Ï ¹Ù²Ü ¼ö´Â ¾ø´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù.
½ºÆÄÀÌ ÇÔ¼ö´Â È£ÃâÇÏ´Â ÇÔ¼ö¿¡ ´ëÇØ ¾Ë¾Æ¾ß¸¸ ÇÑ´Ù. ¶ÇÇÑ °¡·Îä±â ·çƾÀÇ ÇüÅ°¡ ¿øº» WINAPI
ÇÔ¼öÀÇ ¿øÇü°ú ÀÏÄ¡ÇØ¾ß ÇÑ´Ù´Â °ÍÀº ¸Å¿ì Áß¿äÇÏ´Ù. ±×·¸Áö ¾ÊÀ¸¸é ½ºÅÃÀÌ ¼Õ»óµÉ °ÍÀÌ´Ù. ¿¹¸¦ µé¸é CModuleScope
´Â MyTextOutA(),MyTextOutW(),MyExitProcess()
, 3°³ÀÇ Àü¿ª ÇÔ¼ö¸¦ ±¸ÇöÇÏ°í ÀÖ´Ù. HookTool.DLLÀÌ ¾î¶² ÇÁ·Î¼¼½ºÀÇ ÁÖ¼Ò °ø°£¿¡ ·ÎµåµÇ°í ½ºÆÄÀÌ ¿£ÁøÀÌ È°¼ºÈ µÇ¸é ¿øº» TextOutA()
ÀÇ È£ÃâÀÌ ¿äûµÉ ¶§ ¸¶´Ù CModuleScope:: MyTextOutA()
°¡ ´ë½Å È£ÃâµÈ´Ù.
Proposed design of the spying engine itself is quite efficient and offers great flexibility. However, it is suitable mostly for scenarios where the set of functions for interception is known in advance and their number is limited.
½ºÆÄÀÌ ¿£Áø ÀÚüÀÇ ¼³°è´Â ¸Å¿ì È¿À²ÀûÀÌ°í »ó´çÇÑ À¯¿¬¼ºÀ» Á¦°øÇÑ´Ù. ÇÏÁö¸¸ °¡·Îä·Á´Â ÇÔ¼ö¸¦ ¹Ì¸® ¾Ë ¼ö ÀÖ´Â °æ¿ì¿¡ ÀûÇÕÇѵ¥ ±×·¯ÇÑ ÇÔ¼öÀÇ ¼ö´Â ÇÑÁ¤µÇ¾î ÀÖ´Ù.
If you want to add new hooks to the system you simply declare and implement the interception function as I did with MyTextOutA/W()
and MyExitProcess()
. Then you have to register it in the way shown by InitializeHookManagement() implementation.
½Ã½ºÅÛ¿¡ »õ·Î¿î ÈÄÅ©¸¦ Ãß°¡ÇÏ·Á¸é »ùÇÃÀÇ MyTextOutA/W()
¿Í MyExitProcess()
ó·³ ´Ü¼øÈ÷ °¡·Îä±â ÇÔ¼ö¸¦ ¼±¾ðÇÏ°í ±¸ÇöÇϱ⸸ ÇÏ¸é µÈ´Ù. ±×¸®°í ³ª¼ InitializeHookManagement()ÀÇ ±¸Çö¿¡¼ ó·³ ±× ÇÔ¼ö¸¦ µî·ÏÇÏ¿©¾ß ÇÑ´Ù.
Intercepting and tracing process execution is a very useful mechanism for implementing systems that require manipulations of external processes. Notifying interested parties upon starting of a new processes is a classic problem of developing process monitoring systems and system-wide hooks. The Win32 API provides a set of great libraries (PSAPI and ToolHelp [16]) that allow you to enumerate processes currently running in the system. Although these APIs are extremely powerful they don't permit you to get notifications when a new process starts or ends up. Luckily, NT/2K provides a set of APIs, documented in Windows DDK documentation as "Process Structure Routines" exported by NTOSKRNL. One of these APIs PsSetCreateProcessNotifyRoutine()
offers the ability to register system-wide callback function which is called by OS each time when a new process starts, exits or has been terminated. The mentioned API can be employed as a simple way to for tracking down processes simply by implementing a NT kernel-mode driver and a user mode Win32 control application. The role of the driver is to detect process execution and notify the control program about these events. The implementation of the Windows process's observer NTProcDrv provides a minimal set of functionalities required for process monitoring under NT based systems. For more details see articles [11] and [15]. The code of the driver can be located in the NTProcDrv.c file. Since the user mode implementation installs and uninstalls the driver dynamically the currently logged-on user must have administrator privileges. Otherwise you won't be able to install the driver and it will disturb the process of monitoring. A way around is to manually install the driver as an administrator or run HookSrv.exe using offered by Windows 2K "Run as different user" option.
ÇÁ·Î¼¼½ºÀÇ ½ÇÇàÀ» °¡·Îä°í ÃßÀûÇÏ´Â °ÍÀº ¿ÜºÎ ÇÁ·Î¼¼½º¸¦ Á¶ÀÛÇÏ´Â ½Ã½ºÅÛÀ» ±¸ÇöÇÏ´Â °Í¿¡ ¸Å¿ì À¯¿ëÇÑ ¸ÞÄ«´ÏÁòÀÌ´Ù. °ü½ÉÀÌ ÀÖ´Â »õ·Î¿î ÇÁ·Î¼¼½ºÀÇ ½ÃÀÛÀ» ÅëÁöÇÏ´Â °ÍÀº ÇÁ·Î¼¼½º °¨½Ã ½Ã½ºÅÛ°ú ½Ã½ºÅÛ Àü¿ª ÈÄÅ©¸¦ °³¹ßÇÒ ¶§ Á¦±âµÇ´Â °íÀüÀûÀÎ ¹®Á¦ÀÌ´Ù. Win32 API´Â ÇöÀç ½Ã½ºÅÛ¿¡¼ ½ÇÇàµÇ°í ÀÖ´Â ÇÁ·Î¼¼½º¸¦ ³ª¿ÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â °·ÂÇÑ ¶óÀ̺귯¸®(PSAPI¿Í ToolHelp [16])¸¦ Á¦°øÇÑ´Ù. ÀÌ APIµéÀÌ ¸Å¿ì °·ÂÇÏÁö¸¸ »õ·Î¿î ÇÁ·Î¼¼½º°¡ »ý¼ºµÇ°Å³ª ¼Ò¸êÇÏ´Â °ÍÀ» ÅëÁöÇÏÁö´Â ¸øÇÑ´Ù. ´ÙÇེ·´°Ôµµ NT/2K´Â À©µµ¿ìÁî DDK ¹®¼¿¡ ¼³¸íµÇ¾î ÀÖ°í NTOSKRNL¿¡ ÀͽºÆ÷Æ®µÇ¾î ÀÖ´Â "Process Structure Routines"¶ó´Â ÀÏ·ÃÀÇ APIµéÀ» Á¦°øÇÑ´Ù. ÀÌ API ÁßÀÇ ÇϳªÀÎ PsSetCreateProcessNotifyRoutine()
Àº »õ·Î¿î ÇÁ·Î¼¼½º°¡ »ý¼ºµÇ°Å³ª Á¾·á, °Á¦ Á¾·áµÉ ¶§¸¶´Ù OS°¡ È£ÃâÇÏ´Â ½Ã½ºÅÛ Àü¿ª Äݹé ÇÔ¼ö¸¦ µî·ÏÇÒ ¼ö ÀÖµµ·Ï ÇØÁØ´Ù. ÀÌ API´Â NT Ä¿³Î ¸ðµå µå¶óÀ̹ö¿Í »ç¿ëÀÚ ¸ðµå Win32 Á¦¾î ¾îÇø®ÄÉÀ̼ÇÀ» ±¸ÇöÇÏ¿© ½±°Ô ÇÁ·Î¼¼½º¸¦ ÃßÀûÇÒ ¼ö ÀÖ´Â ¹æ¹ýÀ¸·Î »ç¿ëµÉ ¼ö ÀÖ´Ù. µå¶óÀ̹öÀÇ ¿ªÇÒÀº ÇÁ·Î¼¼½ºÀÇ ½ÇÇàÀ» °¨ÁöÇÏ¿© ÀÌ À̺¥Æ®¸¦ Á¦¾î ÇÁ·Î±×·¥¿¡ ÅëÁöÇÏ´Â ¿ªÇÒÀ» ÇÑ´Ù. À©µµ¿ìÁî ÇÁ·Î¼¼½º °¨½ÃÀÚÀÎ NTProcDrv´Â NT ȯ°æ¿¡¼ ÇÁ·Î¼¼½º °¨½Ã¸¦ ¼öÇàÇϱ⿡ ÇÊ¿äÇÑ ÃÖ¼ÒÇÑÀÇ ±â´ÉµéÀ» Á¦°øÇϵµ·Ï ±¸ÇöµÇ¾ú´Ù. º¸´Ù ÀÚ¼¼ÇÑ ³»¿ëÀº ·¹ÆÛ·±½ºÀÇ [11]°ú [15]ÀÇ ±ÛÀ» ÂüÁ¶Çϱ⠹ٶõ´Ù. µå¶óÀ̹öÀÇ ÄÚµå´Â NTProcDrv.c ÆÄÀÏ¿¡ ÀÖ´Ù. »ç¿ëÀÚ ¸ðµå ÇÁ·Î±×·¥ÀÌ µå¶óÀ̹ö¸¦ µ¿ÀûÀ¸·Î ¼³Ä¡, »èÁ¦¸¦ ÇϹǷΠÇöÀç ·Î±×¿ÂÇÑ »ç¿ëÀÚ´Â °ü¸®ÀÚ ±ÇÇÑÀ» °¡Á®¾ß ÇÑ´Ù. ±×·¸Áö ¾ÊÀ¸¸é µå¶óÀ̹ö¸¦ ¼³Ä¡ÇÒ ¼ö ¾ø°í ÇÁ·Î¼¼½º¸¦ °¨½ÃÇÒ ¼ö ¾øÀ» °ÍÀÌ´Ù. ´Ù¸¥ ¹æ¹ýÀ¸·Î´Â °ü¸®Àڷμ µå¶óÀ̹ö¸¦ ¼öµ¿À¸·Î ¼³Ä¡Çϰųª À©µµ¿ìÁî 2K¿¡¼ Á¦°øÇÏ´Â "´Ù¸¥ »ç¿ëÀÚ·Î ½ÇÇàÇϱâ" ¿É¼ÇÀ¸·Î HookSrv.exe¸¦ ½ÇÇàÇÏ´Â °ÍÀÌ ÀÖ´Ù.
Last but not least, the provided tools can be administered by simply changing the settings of an INI file (i.e. HookTool.ini). This file determines whether to use Windows hooks (for 9x and NT/2K) or CreateRemoteThread()
(only under NT/2K) for injecting. It also offers a way to specify which process must be hooked up and which shouldn't be intercepted. If you would like to monitor the process there is an option (Enabled) under section [Trace] that allows to log system activities. This option allows you to report rich error information using the methods exposed by CLogFile class. In fact ClogFile provides thread-safe implementation and you don't have to take care about synchronization issues related to accessing shared system resources (i.e. the log file). For more details see CLogFile and content of HookTool.ini file.
¸¶Áö¸·ÀÌÁö¸¸ »ç¼ÒÇÏÁö ¾ÊÀº °ÍÀÌ Á¦°øµÇ´Â µµ±¸µéÀÌ ´Ü¼øÈ÷ INI ÆÄÀÏ(i.e. HookTool.ini)ÀÇ ¼³Á¤À» ¹Ù²ÞÀ¸·Î¼ °ü¸®µÈ´Ù´Â °ÍÀÌ´Ù. ÀÌ ÆÄÀÏÀº ħÅõ¸¦ À§ÇØ À©µµ¿ìÁî ÈÄÅ©(9x,NT/2K)¸¦ »ç¿ëÇÒ °ÍÀΰ¡ ¾Æ´Ï¸é CreateRemoteThread()
(NT/2K¿¡¼¸¸)¸¦ »ç¿ëÇÒ °ÍÀΰ¡¸¦ °áÁ¤ÇÑ´Ù. ¶ÇÇÑ ¾î¶² ÇÁ·Î¼¼½º¸¦ ÈÄÅ©ÇÏ°í ¾î¶² ÇÁ·Î¼¼½º´Â ÈÄÅ©ÇÏÁö ¾ÊÀ» °ÍÀΰ¡¸¦ ¼³Á¤ÇÒ ¼öµµ ÀÖ´Ù. ¸¸ÀÏ ÇÁ·Î¼¼½º¸¦ °¨½ÃÇϱ⸦ ¿øÇÑ´Ù¸é [Trace] ¼½¼ÇÀÇ (Enabled)ÀÇ °ªÀ» ¼¼ÆÃÇÏ¿© ½Ã½ºÅÛÀÇ È°µ¿À» ±â·ÏÇÒ ¼öµµ ÀÖ´Ù. ÀÌ ¿É¼ÇÀº CLogFile Ŭ·¡½ºÀÇ ¸Þ¼Òµå¸¦ »ç¿ëÇÏ¿© »ó¼¼ÇÑ ¿À·ù Á¤º¸¸¦ ±â·ÏÇÑ´Ù. ½ÇÁ¦·Î CLogFile Ŭ·¡½º´Â thread-safeÇÏ°Ô ±¸ÇöµÇ¾ú°í °øÀ¯ ½Ã½ºÅÛ ÀÚ¿ø(Áï ·Î±× ÆÄÀÏ) Á¢±Ù°ú °ü·ÃµÈ µ¿±âÈ ¹®Á¦¿¡ ½Å°æ¾²Áö ¾Ê¾Æµµ µÈ´Ù. º¸´Ù ÀÚ¼¼ÇÑ ³»¿ëÀº CLogFile°ú HookTool.ini ÆÄÀÏÀ» ÂüÁ¶Çϱ⠹ٶõ´Ù.
The project compiles with VC6++ SP4 and requires Platform SDK. In a production Windows NT environment you need to provide PSAPI.DLL in order to use provided CTaskManager
implementation.
ÀÌ ÇÁ·ÎÁ§Æ®´Â VC6++ SP4¿¡¼ ÄÄÆÄÀϵǰí Ç÷§Æû SDK¸¦ ÇÊ¿ä·Î ÇÑ´Ù. À©µµ¿ìÁî NT ȯ°æ¿¡¼ ½ÇÇàµÇ´Â °æ¿ì CTaskManager
ÀÇ ±¸ÇöÀ» »ç¿ëÇϱâ À§ÇØ PSAPI.DLLÀÌ ÇÊ¿äÇÏ´Ù.
Before you run the sample code make sure that all the settings in HookTool.ini file have been set according to your specific needs.
¿¹Á¦ Äڵ带 ½ÇÇàÇϱâ Àü¿¡ ƯÁ¤ ¿ä±¸ »çÇ׿¡ ¸Â°Ô HookTool.ini ÆÄÀÏÀÌ Á¦´ë·Î ¼³Á¤µÇ¾ú´Â°¡¸¦ È®ÀÎÇØ¾ß ÇÑ´Ù.
For those that will like the lower-level stuff and are interested in further development of the kernel-mode driver NTProcDrv code, they must install Windows DDK.
Àú¼öÁØÀÇ ¹æ¹ýÀ» ¼±È£ÇÏ°í Ä¿³Î ¸ðµå µå¶óÀ̹ö NTProcDrvÀÇ Äڵ带 °³¹ßÇÒ °èȹÀ̶ó¸é À©µµ¿ìÁî DDK°¡ ¿ä±¸µÈ´Ù.
For the sake of simplicity these are some of the subjects I intentionally left out of the scope of this article:
´Ü¼øÇÏ°Ô Çϱâ À§ÇØ ¾Æ·¡ÀÇ ÁÖÁ¦µéÀº À̱ۿ¡¼ ´Ù·çÁö ¾Ê¾Ò´Ù:
This article by far doesn't provide a complete guide for the unlimited API hooking subject and without any doubt it misses some details. However I tried to fit in this few pages just enough important information that might help those who are interested in user mode Win32 API spying.
ÀÌ ±ÛÀº Àý´ë·Î ¹«Á¦ÇÑÀÇ API ÈÄÅ·¿¡ ´ëÇÑ ¿Ïº®ÇÑ °¡À̵尡 ¾Æ´Ï¸ç ÀǽÉÇÒ ¹Ù ¾øÀÌ ÀϺΠÀÚ¼¼ÇÑ ³»¿ëµéÀÌ ºüÁ®ÀÖ´Ù. ÇÏÁö¸¸ ¸î ÆäÀÌÁöÀÇ ±Û¿¡ »ç¿ëÀÚ ¸ðµå Win32 API ÈÄÅ·¿¡ °ü½ÉÀÌ ÀÖ´Â »ç¶÷µéÀÌ Áß¿äÇÑ Á¤º¸¸¦ Áֱ⿡ ÃæºÐÇϵµ·Ï ³ë·ÂÇÏ¿´´Ù.
[1] "Windows 95 System Programming Secrets", Matt Pietrek
[2] "Programming Application for MS Windows" , Jeffrey Richter
[3] "Windows NT System-Call Hooking" , Mark Russinovich and Bryce Cogswell, Dr.Dobb's Journal January 1997
[4] "Debugging applications" , John Robbins
[5] "Undocumented Windows 2000 Secrets" , Sven Schreiber
[6] "Peering Inside the PE: A Tour of the Win32 Portable Executable File Format" by Matt Pietrek, March 1994
[7] MSDN Knowledge base Q197571
[8] PEview Version 0.67 , Wayne J. Radburn
[9] "Load Your 32-bit DLL into Another Process's Address Space Using INJLIB" MSJ May 1994
[10] "Programming Windows Security" , Keith Brown
[11] "Detecting Windows NT/2K process execution" Ivo Ivanov, 2002
[12] "Detours" Galen Hunt and Doug Brubacher
[13a] "An In-Depth Look into the Win32 PE file format" , part 1, Matt Pietrek, MSJ February 2002
[13b] "An In-Depth Look into the Win32 PE file format" , part 2, Matt Pietrek, MSJ March 2002
[14] "Inside MS Windows 2000 Third Edition" , David Solomon and Mark Russinovich
[15] "Nerditorium", James Finnegan, MSJ January 1999
[16] "Single interface for enumerating processes and modules under NT and Win9x/2K." , Ivo Ivanov, 2001
[17] "Undocumented Windows NT" , Prasad Dabak, Sandeep Phadke and Milind Borate
[18] Platform SDK: Windows User Interface, Hooks